[Python] Add total rows to Pandas DataFrame

One of the following two is recommended.

def append_sum_row(df):
    return df.append(df.sum(numeric_only=True), ignore_index=True)
def append_sum_row_label(df):
    df.loc['Total'] = df.sum(numeric_only=True)
    return df

sample

If you try using each, it will be as follows. I think it is better to use it properly depending on whether or not the row label (index) is used.

sample


import pandas as pd


def append_sum_row(df):
    return df.append(df.sum(numeric_only=True), ignore_index=True)

def append_sum_row_label(df):
    df.loc['Total'] = df.sum(numeric_only=True)
    return df

data_frame = pd.DataFrame([
    ["A", 100, 200],
    ["B", 300, 400],
    ["C", 500, 600]
])

print("append_sum_row:", append_sum_row(data_frame), sep="\n")
print("append_sum_row_label:", append_sum_row_label(data_frame), sep="\n")

print


append_sum_row:
     0      1       2
0    A  100.0   200.0
1    B  300.0   400.0
2    C  500.0   600.0
3  NaN  900.0  1200.0

append_sum_row_label:
         0      1       2
0        A  100.0   200.0
1        B  300.0   400.0
2        C  500.0   600.0
Total  NaN  900.0  1200.0

Recommended Posts

[Python] Add total rows to Pandas DataFrame
[Python] How to add rows and columns to a table (pandas DataFrame)
Python application: Pandas # 3: Dataframe
[python] Create table from pandas DataFrame to postgres
Add totals to rows and columns in pandas
[Python] Convert list to Pandas [Pandas]
Export pandas dataframe to excel
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
A handy function to add a column anywhere in a Pandas DataFrame
Python hand play (Pandas / DataFrame beginning)
[Python] Operation memo of pandas DataFrame
[Python] How to use Pandas Series
Add Python 2.7 Japanese documentation to Dash.app
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
Add TRACE log level to Python ...?
[Introduction to Python] Let's use pandas
python / pandas / dataframe / How to get the simplest row / column / index / column
[Python] What is pandas Series and DataFrame?
[Python] Summary of how to use pandas
Python application: Pandas Part 4: DataFrame concatenation / combination
How to reassign index in pandas dataframe
[Pandas] Expand the character string to DataFrame
[Python] Pandas to fully understand in 10 minutes
[Pandas_flavor] Add a method of Pandas DataFrame
Add Gaussian noise to images with python2.7
How to add python module to anaconda environment
Adding Series to columns in python pandas
Add a Python virtual environment to VSCode
[Python] Add comments to standard input files
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
Want to add type hints to your Python decorator?
Updated to Python 2.7.9
My pandas (python)
Just add the python array to the json data
[Python] Road to a snake charmer (6) Manipulate Pandas
Add diagonal lines (hatch) to heatmap (python, matplotlib)
[Python pandas] Create an empty DataFrame from an existing DataFrame
Python pandas: Search for DataFrame using regular expressions
[Python] How to read excel file with pandas
Delete rows with arbitrary values in pandas DataFrame
How to add a Python module search path
To add a module to python put in Julialang
Remove rows with duplicate indexes in pandas DataFrame
Speed: Add element to end of Python array
python pandas notes
Convert pandas dataframe elements to regular string type
"Backport" to python 2
Basic operation of Python Pandas Series and Dataframe (1)
[Python] How to run Jupyter-notebook + pandas + multiprocessing (Pool) [pandas] Memo
[Python] [Django] How to use ChoiceField and how to add options
How to add help to HDA (with Python script bonus)
Summary of pre-processing practices for Python beginners (Pandas dataframe)
Convert comma-separated numeric strings to numbers in Pandas DataFrame
How to convert JSON file to CSV file with Python Pandas
[Python] How to deal with pandas read_html read error
[Python] Summary of table creation method using DataFrame (pandas)
How to add page numbers to PDF files (in Python)
[Python] A memo to write CSV vertically with Pandas
[Python] pandas Code that is likely to be reused
I tried to summarize how to use pandas in python