Given a DataFrame A
, I want to sum the columns in the same category, and put the result in new columns in A_modified
.
A=
location exp1 exp2 data1 data2
0 FL 100 20 30 10
1 NC 40 30 50 60
A_modified
location exp1 exp2 data1 data2 total_exp total_data
0 FL 100 20 30 10 120 40
1 NC 40 30 50 60 70 110
I want to do it for multiple DataFrames all having the same columns, what is the best practice to do it? Here is what I did, but I would think that using dictionaries would be better to deal with more columns.
def f(df):
df['exp_sum']= pd.Series(df.filter(like='exp').sum(axis=1), index = df.index)
df['data_sum']= pd.Series(df.filter(like='data').sum(axis=1), index = df.index)
return df
A = f(A)