0

I know this question is similar to this but my question is to have the sum of all values as I am fairly new to pandas and I don't have any Idea to pull this off.

currently, I can sum all the value of columns and add it on the df with the following code

df.loc[:,'x':'y'].sum(axis = 1)

+---+---+---+-------+
| x | y | z | total |
+---+---+---+-------+
| 1 | 2 | 1 |     4 |
| 0 | 1 | 1 |     2 |
| 1 | 0 | 0 |     1 |
+---+---+---+-------+

is there a way to have the sum of all columns and place it on the start of the dataframe?

+-------+---+---+---+
| total | x | y | z |
+-------+---+---+---+
|     4 | 1 | 2 | 1 |
|     2 | 0 | 1 | 1 |
|     1 | 1 | 0 | 0 |
+-------+---+---+---+
Led
  • 662
  • 1
  • 19
  • 41
  • 1
    `df.insert(0,'total',df.loc[:,'x':'y'].sum(axis = 1))` – anky Jun 20 '19 at 14:42
  • thank you, please answer so I can mark it. – Led Jun 20 '19 at 14:45
  • You are welcome. Unfortunately it is a dupe, so i will let it be. :) – anky Jun 20 '19 at 14:45
  • 1
    well it is different from just inserting at any place. Or I'm just fairly new to Pandas and I don't know what I'm doing. But thank you very much for the answer. – Led Jun 20 '19 at 14:48

0 Answers0