0

In a dataframe I have 24-Columns: h0, h1, ... h23. I can sum them by df['h0']+df['h1']+...+df['h23'].

How can I do it with a loop?

Edit:

I forgot to say, that there are other columns than h0, h1, ... h23.

sreved
  • 13
  • 3

1 Answers1

2

You can do:

df.sum(1)

to sum along the rows on all columns. Or if you have a sublist of columns col_list = ['h0', 'h2', 'h3', 'h23']:

df[col_list].sum(1)
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74