0

I currently have the following code:

import glob
import pandas as pd

path_pattern = 'C:/Users/Joey/Desktop/GC results/Results/FID_00*'
files = glob.glob(path_pattern)
dataframes = [pd.DataFrame.from_csv(f, index_col=None) for f in files]

new_df = pd.DataFrame()

for i in dataframes:
    selected_data = i['Unnamed: 3'].ix[12:16]  
    new_df['Run'] = selected_data
    print new_df

out:

       Run
12  5187666.22
13  1453339.93
14   193334.09
15   157630.92
16    98943.96
           Run
12  5188329.28
13  1455640.31
14      193074
15   157420.83
16    98791.72
           Run
12  5188943.17
13  1456575.95
14   192977.15
15   157325.56
16    98699.43
           Run
12   5188675.1
13  1456622.43
14   192796.99
15   157174.61
16    98598.53
           Run
12  5187783.26
13  1456612.29
14   192703.05
15   157078.52
16    98511.48

At the moment, the selections are all in the same column. Is it possible to reorganize this such that each selection 12-16 is a separate column? I would like run1, run2, ..., run6 to be 6 separate columns.

5
  • Did you check in the Pandas documentation? It explains in detail how to select rows and columns. Commented Jun 10, 2015 at 11:41
  • Just rename the colum Run in new_df['Run'] everytime you are adding a new column to new_dfas shown in my answer. Commented Jun 10, 2015 at 13:01
  • This doesn't loop through all 6 dataframes I previously made. It gives me df1 * 6 columns Commented Jun 10, 2015 at 13:06
  • Did you try my edited code below? Commented Jun 10, 2015 at 16:13
  • Yes it worked.. but i had to change selected_data = i['Unnamed: 3'].ix[12:16] .... the i before the [ to df .. many thanks :) Commented Jun 11, 2015 at 12:41

1 Answer 1

1

Select desired data using using .ix and add selected data (type: pd.Series) to a new dataframe:

# create new dataframe for selected data
new_df = pd.DataFrame()

# placeholder for six selections (i = 1...6)
for i, df in enumerate(dataframes):
    colname = 'Run {}'.format(i+1)
    selected_data = i['Unnamed: 3'].ix[12:16]  
    new_df[colname] = selected_data
    print new_df  
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this. However adds dataframe 1 to all 6 columns. How would I add 6 different dataframes to the 6 different columns?
Just rename the colum Run in new_df['Run'] everytime you are adding a new column to new_dfas shown in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.