选项1
使用
assign和
drop
In [644]: cols = ['B', 'C', 'D']In [645]: df.assign(E=df[cols].sum(1)).drop(cols, 1)Out[645]: A E0 a 42.01 b 52.02 c 31.03 d 2.04 e 62.05 f 70.0
选项2
使用分配和
drop
In [648]: df['E'] = df[cols].sum(1)In [649]: df = df.drop(cols, 1)In [650]: dfOut[650]: A E0 a 42.01 b 52.02 c 31.03 d 2.04 e 62.05 f 70.0
选项3 最近,我喜欢第3个选项。
使用
groupby
In [660]: df.groupby(np.where(df.columns == 'A', 'A', 'E'), axis=1).first() #or sum max minOut[660]: A E0 a 42.01 b 52.02 c 31.03 d 2.04 e 62.05 f 70.0In [661]: df.columns == 'A'Out[661]: array([ True, False, False, False], dtype=bool)In [662]: np.where(df.columns == 'A', 'A', 'E')Out[662]:array(['A', 'E', 'E', 'E'], dtype='|S1')



