Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would it be possible to create multiple dataframes in one go?

Tags:

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)

Can I run the following code in one go for df, df10, df20? Btw, in the real data I will run 80 dataframes with the same code as below;

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]
like image 485
Zanshin Avatar asked Feb 20 '26 11:02

Zanshin


1 Answers

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

like image 176
piRSquared Avatar answered Feb 23 '26 00:02

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!