0

I'm trying to find how to get sum of all columns or for specific column

I found it and here is that part of code

data.loc['total'] = data.select_dtypes(np.number).sum() 

this works correctly but I get warning

C:\Users\AAR\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexing.py:671: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)

table

on attached image you can see what I want to get

cs95
  • 379,657
  • 97
  • 704
  • 746
radojicic
  • 27
  • 5

1 Answers1

0

If you just want to do a sum, you should just do this:

Dataframe:

     0   1   2   3   4   5     6     7     8     9
0   12  32  45  67  89  54  23.0  56.0  78.0  98.0
1   34  76  34  89  34   3   NaN   NaN   NaN   NaN
2   76  34  54  12  43  78  56.0   NaN   NaN   NaN
3   76  56  45  23  43  45  67.0  76.0  67.0   8.0
4   87   9   9   0  89  90   6.0  89.0   NaN   NaN
5   23  90  90  32  23  34  56.0   9.0  56.0  87.0
6   23  56  34   3   5   8   7.0   6.0  98.0   NaN
7   32  23  34   6  65  78  67.0  87.0  89.0  87.0
8   12  23  34  32  43  67  45.0   NaN   NaN   NaN
9  343  76  56   7   8   9   4.0   5.0   8.0  68.0

df = df.append(df.sum(numeric_only=True), ignore_index=True)

Output:

        0      1      2      3      4      5      6      7      8      9
0    12.0   32.0   45.0   67.0   89.0   54.0   23.0   56.0   78.0   98.0
1    34.0   76.0   34.0   89.0   34.0    3.0    NaN    NaN    NaN    NaN
2    76.0   34.0   54.0   12.0   43.0   78.0   56.0    NaN    NaN    NaN
3    76.0   56.0   45.0   23.0   43.0   45.0   67.0   76.0   67.0    8.0
4    87.0    9.0    9.0    0.0   89.0   90.0    6.0   89.0    NaN    NaN
5    23.0   90.0   90.0   32.0   23.0   34.0   56.0    9.0   56.0   87.0
6    23.0   56.0   34.0    3.0    5.0    8.0    7.0    6.0   98.0    NaN
7    32.0   23.0   34.0    6.0   65.0   78.0   67.0   87.0   89.0   87.0
8    12.0   23.0   34.0   32.0   43.0   67.0   45.0    NaN    NaN    NaN
9   343.0   76.0   56.0    7.0    8.0    9.0    4.0    5.0    8.0   68.0
10  718.0  475.0  435.0  271.0  442.0  466.0  331.0  328.0  396.0  348.0
NYC Coder
  • 7,424
  • 2
  • 11
  • 24
  • this is fantastic, thanks a lot, do you maybe know how to do this for example just for column 5 instead of for all columns? – radojicic May 17 '20 at 20:56
  • You can select the columns you want like this: `cols = ['0', '1', '2', '3', '4'] df = df[cols].append(df[cols].sum(numeric_only=True), ignore_index=True) print(df)` – NYC Coder May 17 '20 at 21:03
  • nice, thanks a lot for yoyr response and help – radojicic May 17 '20 at 23:34