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

Viewed 34

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

enter image description here

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)

enter image description here 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?

1 Answers

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
Related