是否可以一次创建多个数据帧?

时间:2016-11-23 19:25:34

标签: python pandas

df = pd.DataFrame( {
   'A': ['d','d','d','f','f','f','g','g','g','h','h','h'],
   'B': [5,5,6,7,5,6,6,7,7,6,7,7],
   'C': [1,1,1,1,1,1,1,1,1,1,1,1],
   'S': [2012,2013,2014,2015,2016,2012,2013,2014,2015,2016,2012,2013]     
    } );

df = (df.B + df.C).groupby([df.A,df.S]).agg(['sum','size']).
      unstack(fill_value=0)
df10 = (df.B * df.C).groupby([df.A,df.S]).agg(['sum','size']).
      unstack(fill_value=0)
df20 = (df.B - df.C).groupby([df.A,df.S]).agg(['sum','size']).
      unstack(fill_value=0)

我可以为df,df10,df20一次运行以下代码吗?顺便说一句,在实际数据中,我将使用与下面相同的代码运行80个数据帧;

df1 = df.groupby(level=0, axis=1).sum()
new_cols= list(zip(df1.columns.get_level_values(0),['total'] *     len(df.columns)))
df1.columns = pd.MultiIndex.from_tuples(new_cols)
df2 = pd.concat([df1,df], axis=1).sort_index(axis=1).sort_index(axis=1,  level=1)
df2.columns = ['_'.join((col[0], str(col[1]))) for col in df2.columns]

1 个答案:

答案 0 :(得分:1)

b_c_idx_locs = [df.columns.get_loc('B'), df.columns.get_loc('C')]

a = df.values[:, b_c_idx_locs]

df['B+C'] = a.sum(1)
df['B*C'] = a.prod(1)
df['B-C'] = -np.diff(a)
cols = ['B+C', 'B*C', 'B-C']

df.groupby(['A', 'S'])[cols].agg(['sum', 'size'])

enter image description here