简体   繁体   English

添加两个 pandas dataframe 列,其区别仅在于后缀参数,例如“A_x”、“A_y”,并将这两列重命名为“A”

[英]add two pandas dataframe columns which differs by only suffix parameter for e.g., “A_x”, “A_y” and rename these two columns addition with “A”

How to add two pandas dataframe columns which differs by only suffix parameter for eg, "A_x", "A_y" and rename these two columns addition with "A".如何添加两个 pandas dataframe 列,它们的区别仅在于后缀参数,例如“A_x”、“A_y”,并将这两列重命名为“A”。

For eg, I have a data like this enter image description here例如,我有这样的数据在此处输入图像描述

The columns must be renamed without any of the suffix ie., to CT_1 or CT_2 etc....列必须在没有任何后缀的情况下重命名,即,重命名为 CT_1 或 CT_2 等....

Use:利用:

df = pd.DataFrame([np.arange(6)], columns=['a','s','CT_1_x','CT_1_y','CT_2_x','CT_2_y'])
print (df)
   a  s  CT_1_x  CT_1_y  CT_2_x  CT_2_y
0  0  1       2       3       4       5

df = df.set_index(['a','s']).groupby(lambda x: x.rsplit('_', 1)[0], axis=1).sum().reset_index()
print (df)
   a  s  CT_1  CT_2
0  0  1     5     9

To add the two columns添加两列

df['A'] = df['A_x'] + df['A_y']

and if you want to remove the original columns如果你想删除原来的列

df.drop(columns = ['A_x','A_y'])

If you have too many such columns col2sum = ['A_1', 'A_2', ...] to type by hand, the best way would be to melt the df into a long form.如果您有太多这样的列col2sum = ['A_1', 'A_2', ...]无法手动输入,最好的方法是将 df 合并为长格式。

dfm = melt(df, id_vars = ???, value_vars = col2sum)

and go from there (eg groupby ).和 go 从那里(例如groupby )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM