colibro
colibro

Reputation: 45

group and sum certain columns filtered by name and combine in one column

Say I have the following dataframe:

import pandas as pd

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

The dataframe created above

I want to sum up columns X_1, X_2, X_3 into a new column Col_2 within the dataframe. I know 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)

Result dataframe of the concat

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?

Upvotes: 2

Views: 46

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195553

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

Upvotes: 2

Related Questions