📅  最后修改于: 2023-12-03 15:03:28.228000             🧑  作者: Mango
Pandas DataFrame.sum()
The DataFrame.sum()
method in the Pandas library is used to calculate the sum of values across different axes in a DataFrame. This function is often used to calculate the total sum of numerical columns or rows.
DataFrame.sum(axis=None, skipna=None, level=None, numeric_only=None, min_count=0)
axis
: It specifies the axis along which the sum operation is performed. By default, it sums the values vertically (axis=0
). To sum horizontally, use axis=1
.skipna
: It is a boolean value that indicates whether to exclude missing or NaN
values when performing the sum operation. By default, it is set to None
, which means missing values are included.level
: It is used when the DataFrame has hierarchical columns. It specifies the level for which the sum is calculated.numeric_only
: It is a boolean value that specifies whether only numeric columns should be included in the summation. By default, it is set to None
.min_count
: It specifies the minimum number of valid values required for the summation. By default, it is set to 0
, which means any number of valid values are considered.The DataFrame.sum()
method returns a new Series
object that contains the sums of values across the specified axis.
Consider the following example DataFrame:
import pandas as pd
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
By default, df.sum()
will return the sum of each column:
# Column-wise sum
df.sum()
| | A | B | C | | ---- | -- | -- | -- | | Sum | 6 | 15 | 24 |
To calculate the sum row-wise, use axis=1
:
# Row-wise sum
df.sum(axis=1)
| | Sum | | -- | --- | | 0 | 12 | | 1 | 15 | | 2 | 18 |
You can exclude missing values from the sum calculation by setting skipna=True
:
# Exclude missing values
df.sum(skipna=True)
| | A | B | C | | ---- | -- | -- | -- | | Sum | 6 | 15 | 24 |
It is also possible to calculate the sum for a specific column or a subset of columns:
# Sum for specific columns
df[['A', 'B']].sum()
| | Sum | | ---- | --- | | A | 6 | | B | 15 |
These are just a few examples of how to use the DataFrame.sum()
method in Pandas. It is a powerful function that is often used in data analysis and manipulation tasks.
Note: This is just a brief overview of the
DataFrame.sum()
function. Refer to the Pandas documentation for more detailed information.