0

I have tried to find how to append a Total Row, which sums the columns.

There is a elegant solution to this problem here: [SOLVED] Pandas dataframe total row

However, when using this method, I have noticed a warning message:

FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

I have tried to use this alternative, in order to avoid using legacy code. When I tried to use concat method, it was appending the sum row to the last column vertically.

Code I used:

pd.concat( [df, df.sum(numeric_only=True)] )

Result:

         a       b     c         d         e
0   2200.0   14.30   NaN   2185.70       NaN
1   3200.0   20.80   NaN   3179.20       NaN
2   6400.0   41.60   NaN   6358.40       NaN
3      NaN     NaN   NaN       NaN  11800.00     <-- Appended using Concat
4      NaN     NaN   NaN       NaN     76.70     <-- Appended using Concat
5      NaN     NaN   NaN       NaN      0.00     <-- Appended using Concat
6      NaN     NaN   NaN       NaN  11723.30     <-- Appended using Concat

What I want:

           a       b      c          d
0     2200.0   14.30    NaN    2185.70
1     3200.0   20.80    NaN    3179.20
2     6400.0   41.60    NaN    6358.40
3   11800.00   76.70   0.00   11723.30     <-- Appended using Concat

Is there an elegant solution to this problem using concat method?

Evil_Cheetah
  • 63
  • 1
  • 7

1 Answers1

3

Convert the sum (which is a pandas.Series) to a DataFrame and transpose before concat:

>>> pd.concat([df,df.sum().to_frame().T], ignore_index=True)

         a     b    c        d
0   2200.0  14.3  NaN   2185.7
1   3200.0  20.8  NaN   3179.2
2   6400.0  41.6  NaN   6358.4
3  11800.0  76.7  0.0  11723.3
not_speshal
  • 22,093
  • 2
  • 15
  • 30
  • Thank you very much for the solution. I have tried to Google how to transpose the array, but didn't know you have to use `to_frame`. – Evil_Cheetah Mar 02 '22 at 18:13
  • Thanks a lot for this solutions, in pandas documentations, pd.concat([df1, df1]), doesn't work the same as df1.append(df2) would – Tuhin Mitra Mar 28 '22 at 17:39