简体   繁体   中英

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. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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