组织pandas DataFrame中的列

时间:2015-06-12 16:05:49

标签: python pandas dataframe

我有2组92列。目前,所有92列都在一行中。是否有可能重新编号,使92列分成12组,基本上有8组12个数据(一个在另一个下面)。我的代码:

import glob
import pandas as pd
import os

os.chdir('C:/Users/peaches9/Desktop/')

Result = []

def FID_extract(filepath):

    path_pattern = filepath
    files = glob.glob(path_pattern)
    dataframes = [pd.DataFrame.from_csv(f, index_col=None) for f in files]

    new_dfa = pd.DataFrame()

    for i, df in enumerate(dataframes):
        colname = 'Run {}'.format(i+1)
        selected_data = df['Unnamed: 3'].ix[12:17]  
        new_dfa[colname] = selected_data
    #print new_dfa 
    #new_dfa.to_csv('FID_11169_Liquid.csv') 
    Result.append(new_dfa)


def TCD_extract(filepath):

    path_pattern = filepath
    files = glob.glob(path_pattern)
    dataframes = [pd.DataFrame.from_csv(f, index_col=None) for f in files]

    new_dfb = pd.DataFrame()

    for i, df in enumerate(dataframes):
        colname = 'Run {}'.format(i+1)
        selected_data = df['Unnamed: 3'].ix[12:15]  
        new_dfb[colname] = selected_data
    #print new_dfb 
    #new_dfb.to_csv('TCD_11169_liquid.csv') 
    Result.append(new_dfb)


FID_extract('C:/Users/peaches9/Desktop/Cryostat Verification/GC results/11169_Cryo_1bar/FID_0*') #files directory

TCD_extract('C:/Users/peaches9/Desktop/Cryostat Verification/GC results/11169_Cryo_1bar/TCD_0*')

dfc = pd.concat(Result)

出:

     Run 1.....   Run 95      Run 96  
12  5193791.85  5193915.21  5194343.34  
13  1460874.04  1460929.33  1461072.84  
14   192701.82   192729.55   192743.99  
15    156836.4   156876.97   156889.26  
16    98342.84     98346.7    98374.95  
17         NaN         NaN         NaN  
12     3982.69     3982.16     4017.66  
13  2913008.04  2913627.33   2914075.7  
14   226963.37    226956.1   227106.71  
15     25208.2    25173.89    25197.88  

我希望所有96列分成8 X 12列,彼此相同。提前谢谢了。

编辑:

我已经设法将数据帧分成8组......但是我无法让每个数据帧彼此相互影响。他们总是在右边连着!

dfc = pd.concat(Result)

df1 = dfc.ix[:,0:12]
df2 = dfc.ix[:,12:24]
df3 = dfc.ix[:,24:36]
df4 = dfc.ix[:,36:48]
df5 = dfc.ix[:,48:60]
df6 = dfc.ix[:,60:72]
df7 = dfc.ix[:,72:84]
df8 = dfc.ix[:,84:96]

pieces = [df1,df2,df3,df4,df5,df6,df7,df8]

df_final = pd.concat([df1, df2], levels = 1, axis = 3)

1 个答案:

答案 0 :(得分:0)

假设您正在尝试获取96列,并创建一个包含12列和8倍行的2维DataFrame,那么您需要:

df_final = pd.concat( pieces, axis=0, ignore_index=True )

如果您尝试制作三维DataFrame,并且新维度包含8个值,则不是要创建一个DataFrame,而是Panel