How to select row and column from dataframe using pandas

243 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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