Pandas DataFrame: Appending a Custom Footer Row (4 examples)

Updated: February 23, 2024 By: Guest Contributor Post a comment


DataFrames are one of the most powerful tools in the Pandas library, offering extensive capabilities for data manipulation and analysis. In this tutorial, we’ll explore how to append a custom footer row to a DataFrame in Pandas using multiple examples ranging from basic to advanced. This function can be invaluable when summarizing or highlighting important data at the bottom of your DataFrame.


Before we dive into the examples, ensure you have Pandas installed and imported into your Python environment:

import pandas as pd

Appending a footer to a DataFrame involves adding an additional row at the bottom, often for summarization or annotation purposes. Unlike header rows, footers are not inherently supported in Pandas, but we can implement this functionality with a few workarounds.

Example 1: Basic Footer Row

The simplest way to append a footer to a DataFrame is by using the pd.concat() function. This example creates a basic DataFrame and appends a footer row with custom content.

import pandas as pd

df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'Occupation': ['Engineer', 'Doctor', 'Artist']

footer = pd.DataFrame({
    'Name': ['Footer'],
    'Age': [''],
    'Occupation': ['Summary']
}, index=[3])

df = pd.concat([df, footer])

This will output a DataFrame with a summary row at the bottom:

       Name Age Occupation
0     Alice  25   Engineer
1       Bob  30     Doctor
2   Charlie  35     Artist
3    Footer      Summary

Example 2: Footer with Aggregated Data

Often, the purpose of a footer is to summarize data within the DataFrame. This example demonstrates how to generate aggregated data (e.g., sum, average) and append it as a footer.

df = pd.DataFrame({
    'Sales': [250, 150, 300, 200],
    'Profit': [25, 15, 30, 20]

summary = pd.DataFrame({
    'Sales': [df['Sales'].sum()],
    'Profit': [df['Profit'].sum()]
}, index=['Total'])

df = pd.concat([df, summary])


        Sales  Profit
0        250      25
1        150      15
2        300      30
3        200      20
Total     900      90

Example 3: Styling the Footer for Better Visibility

In some cases, you may want the footer to stand out for better visibility. This can be achieved by styling the DataFrame prior to exporting it. Here’s how:

df = pd.DataFrame({
    'Sales': [250, 150, 300, 200],
    'Profit': [25, 15, 30, 20]

footer = pd.DataFrame({
    'Sales': ['Total Sales:', df['Sales'].sum()],
    'Profit': ['Total Profit:', df['Profit'].sum()]
}, index=['', 'Summary'])

df_styled = pd.concat([df, footer])

# Now, let's apply some basic styling for visibility
style = lambda x: ['font-weight: bold' if in ['Summary'] else '' for i in x]

Note: This styling will show up in Jupyter Notebooks or other environments that support HTML rendering of DataFrames, but not in all output formats.

Example 4: Advanced Customization

For more advanced use cases, you might want to include more complex data or calculations in your footer. Let’s consider a scenario where we calculate both the sum and the average, and append these as two separate footer rows.

df = pd.DataFrame({
    'Sales': [300, 200, 400, 500],
    'Profit': [30, 20, 40, 50]

sum_row = pd.DataFrame({
    'Sales': ['Sum:', df['Sales'].sum()],
    'Profit': ['Sum:', df['Profit'].sum()]
}, index=['', 'Total'])

avg_row = pd.DataFrame({
    'Sales': ['Average:', df['Sales'].mean()],
    'Profit': ['Average:', df['Profit'].mean()]
}, index=['', 'Average'])

# Append both rows
footer_df = pd.concat([sum_row, avg_row])
df = pd.concat([df, footer_df])

Output will feature both summary calculations as separate footer rows:

          Sales         Profit
0          300            30
1          200            20
2          400            40
3          500            50
Total      Sum:          Sum:
            1400          140
Average Average:   Average:
         350            35


Appending a custom footer row in Pandas is a versatile way to summarize or annotate your data effectively. Although Pandas does not inherently support footer rows, the examples provided demonstrate that with creativity and a bit of code, achieving this functionality is entirely within reach. Remember, the key to effective data presentation is clarity, so ensure your footer rows add value and insight to your DataFrame.