Search code examples
pythonpandasdataframesum

How To Create a "Total" Row for One Column in a Pandas Dataframe


So I've created a DF from file names I've pulled using the os module

The file names include dollar amounts and I would like to be able to create a row that totals just the amount in that column of the DF (index 3)

However, when I follow this code structure:

File_Name.loc['Total'] = File_Name.sum()

I get this:

                                                 Invoice  ...                                             Amount
30                                                  6515  ...                                             401.01
Total  0822OH082522KTR1987000084201987000084481987000...  ...  478.88550.0030.1032.3912.0432.521020.4729.1442...

I would love for it to look like this:

         Invoice         Vendor   Amount
30          6515        Expense   401.01
Total                          198556.79

Any help would be much appreciated!


Solution

  • Pandas just released (v1.5.0) a new feature in Styler for doing just this. The Styler is used for display of data whereas a DataFrame is essentially an efficient memory map of data. Therefore the ability the combine and structure different DataFrames for display purposes is useful. The Styler allows for configuring formatting output differently for different tables. E.g. a column might have integer values but the arithmetic mean is usually a float with multiple decimals.

    See the docs for Styler.concat as it discusses this use case. https://pandas.pydata.org/docs/dev/reference/api/pandas.io.formats.style.Styler.concat.html