简体   繁体   English

Pandas df 对按名称过滤的某​​些列进行分组和求和,并合并在一列中

[英]Pandas df group and sum certain columns filtered by name and combine in one columns

Say I have the following frame:假设我有以下框架:

a=pd.DataFrame(np.random.randn(5, 5),columns=["Col_1","X_1","X_2","X_3","Col_3"])
a

在此处输入图像描述

I want to sum up coumns X_1 ,X_2 ,X_3 in an new column Col_2 within the frame.我想在框架内的新列 Col_2 中总结列 X_1 ,X_2 ,X_3 。 I konw that I can do:我知道我能做到:

b=a.filter(like="X")
pd.concat([a.drop(b.columns,axis=1),b.sum(axis=1).rename("Col_2")],axis=1)

在此处输入图像描述 However, I am looking for a more clean and lean one line version of doing this.但是,我正在寻找一种更干净、更精简的单行版本。 Is there possibly something that can be done with .groupby? .groupby 可以做些什么吗?

Try:尝试:

out = df.assign(Col_2=df.loc[:, "X_1":"X_3"].sum(1)).filter(like="Col")

print(out)

Prints:印刷:

      Col_1     Col_3     Col_2
0 -2.306087 -0.698832 -2.824466
1  0.650526 -0.780234 -0.534918
2  1.844277  0.777565 -0.531298
3 -0.424138  0.423905 -2.853805
4  1.236403  0.848035 -1.332700

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

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