How can a total row be added to a Pandas DataFrame?

How can a total row be added to a Pandas DataFrame?

A total row can be added to a Pandas DataFrame by using the “sum” function and specifying the axis as “index”. This will add a new row at the bottom of the DataFrame, displaying the sum of each column’s values. This can be useful for calculating overall totals or subtotals within a DataFrame. Additionally, the “append” function can be used to add a new row to the DataFrame, which can then be filled with the desired values. This allows for more flexibility in creating custom total rows for specific needs.

Add a Total Row to Pandas DataFrame


You can use the following basic syntax to add a ‘total’ row to the bottom of a pandas DataFrame:

df.loc['total']= df.sum()

The following example shows how to use this syntax in practice.

Example: Add a Total Row to Pandas DataFrame

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F'],
                   'assists': [5, 7, 7, 9, 12, 9],
                   'rebounds': [11, 8, 10, 6, 6, 5],
                   'blocks': [6, 6, 3, 2, 7, 9]})

#view DataFrame
print(df)

  team  assists  rebounds  blocks
0    A        5        11       6
1    B        7         8       6
2    C        7        10       3
3    D        9         6       2
4    E       12         6       7
5    F        9         5       9

We can use the following syntax to add a ‘total’ row at the bottom of the DataFrame that shows the sum of values in each column:

#add total row
df.loc['total']= df.sum()

#view updated DataFrame
print(df)

         team  assists  rebounds  blocks
0           A        5        11       6
1           B        7         8       6
2           C        7        10       3
3           D        9         6       2
4           E       12         6       7
5           F        9         5       9
total  ABCDEF       49        46      33

A new row has been added to the bottom of the DataFrame that shows the sum of values in each column.

Note that for character columns, the ‘total’ is simply the concatenation of every character in the column.

If you’d like, you can set the ‘total’ value in the team column to simply be blank:

#set last value in team column to be blank
df.loc[df.index[-1], 'team'] = ''

#view updated DataFrame
print(df)

      team  assists  rebounds  blocks
0        A        5        11       6
1        B        7         8       6
2        C        7        10       3
3        D        9         6       2
4        E       12         6       7
5        F        9         5       9
total            49        46      33

The last value in the team column is now blank, as opposed to being a concatenation of every character in the column.

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How can a total row be added to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-total-row-be-added-to-a-pandas-dataframe/

stats writer. "How can a total row be added to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-a-total-row-be-added-to-a-pandas-dataframe/.

stats writer. "How can a total row be added to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-total-row-be-added-to-a-pandas-dataframe/.

stats writer (2024) 'How can a total row be added to a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-total-row-be-added-to-a-pandas-dataframe/.

[1] stats writer, "How can a total row be added to a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can a total row be added to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top