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

Question:

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?

Asked By: colibro

||

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
Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.