如何根据Python中的名称对列进行分类?

时间:2015-11-24 18:13:05

标签: python dictionary pandas dataframe apply

鉴于DataFrame A,我想对相同类别中的列求和,并将结果放在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

我想为多个具有相同列的DataFrame执行此操作,最佳做法是什么?这就是我所做的,但我认为使用字典会更好地处理更多列。

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)

0 个答案:

没有答案
相关问题