0

I have this DataFrame:

import pandas as pd
d = {'1_col': [1, 2], '2_col': [3, 4], 'var1': [5,6]}
df = pd.DataFrame(data=d)
print(df)

which looks like this:

    1_col  2_col  var1
0     1     3     5
1     2     4     6

I need to sum all the columns whose name ends in _col, so that the resulting DataFrame looks like this (the column called sum is the sum of the columns 1_col and 2_col:

    1_col  2_col  var1 sum
0     1     3     5     4
1     2     4     6     6

Is there a way in pandas to sum all the columns whose name ends with "_col" rather than doing it manually?

1 Answer 1

3

Use df.filter with df.sum:

In [1209]: df['sum'] = df.filter(like='_col').sum(1)

In [1210]: df
Out[1210]: 
   1_col  2_col  var1  sum
0      1      3     5    4
1      2      4     6    6
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.