Pandas DataFrame DataFrame.sum() function
Python Pandas DataFrame.sum() function calculates the DataFrame
sum of the values of the object along the specified axis.
pandas.DataFrame.sum()
Syntax
DataFrame.sum(
axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs
)
parameter
axis |
Sum along rows ( axis=0 ) or columns ( axis=1 ) |
skipna |
Boolean. Exclude NaN value( skipna=True s) or include NaN value( skipna=False s). |
level |
If axis is MultiIndex , counts are taken along a specific level. |
numeric_only |
Boolean. For numeric_only=True , only include the float , , int and boolean columns. |
min_count |
Integer. Minimum number of non-NaN values to sum. If this condition is not met, the sum will be NaN . |
**kwargs |
Additional keyword arguments to functions |
Return Value
If not specified level
, returns the sum of the values along the requested axis Series
, otherwise returns the sum of the values DataFrame
.
Example code: DataFrame.sum()
Method to calculate sum along column axis
import pandas as pd
df = pd.DataFrame({'X':
[1,2,3,4,5],
'Y': [1, 2, 3,4,5],
'Z': [3,4,5,6,3]})
print("DataFrame:")
print(df)
sums=df.sum()
print("Column-wise Sum:")
print(sums)
Output:
DataFrame:
X Y Z
0 1 1 3
1 2 2 4
2 3 3 5
3 4 4 6
4 5 5 3
Column-wise Sum:
X 15
Y 15
Z 21
dtype: int64
It calculates the sum of all columns X
, Y
and Z
, and finally returns a Series
object that includes the sum of each column.
In Pandas, to find the sum of a column in a DataFrame, you just need to call sum()
the sum function on that column.
import pandas as pd
df = pd.DataFrame({'X':
[1,2,3,4,5],
'Y': [1, 2, 3,4,5],
'Z': [3,4,5,6,3]})
print("DataFrame:")
print(df)
sums=df["Z"].sum()
print("Sum of values of Z-column:")
print(sums)
Output:
DataFrame:
X Y Z
0 1 1 3
1 2 2 4
2 3 3 5
3 4 4 6
4 5 5 3
Sum of values of Z-column:
21
It just gives Z
the sum of the DataFrame column values.
Example Code: DataFrame.sum()
Method to Find Sum Along Row Axis
import pandas as pd
df = pd.DataFrame({'X':
[1,2,3,4,5],
'Y': [1, 2, 3,4,5],
'Z': [3,4,5,6,3]})
print("DataFrame:")
print(df)
sums=df.sum(axis=1)
print("Row-wise sum:")
print(sums)
Output:
DataFrame:
X Y Z
0 1 1 3
1 2 2 4
2 3 3 5
3 4 4 6
4 5 5 3
Row-wise sum:
0 5
1 8
2 11
3 14
4 13
dtype: int64
It calculates the sum of all rows and returns a Series
object containing the sum for each row.
In Pandas, if you want to find DataFrame
the sum of a row in , you need to call sum()
the function to calculate the sum of that row.
import pandas as pd
df = pd.DataFrame({'X':
[1,2,3,4,5],
'Y': [1, 2, 3,4,5],
'Z': [3,4,5,6,3]})
print("DataFrame:")
print(df)
sum_3=df.iloc[[2]].sum(axis=1)
print("Sum of values of 3rd Row:")
print(sum_3)
Output:
DataFrame:
X Y Z
0 1 1 3
1 2 2 4
2 3 3 5
3 4 4 6
4 5 5 3
Sum of values of 3rd Row:
2 11
dtype: int64
It only gives DataFrame
the sum of the values in the third row of .
Use iloc
the method to select a row based on its index.
Example code: DataFrame.sum()
Method lookup ignoring NaN
the sum of values
Uses skipna
the default value of the parameter, which skipna=True
finds the sum of along the specified axis DataFrame
and ignores NaN
the value of .
import pandas as pd
df = pd.DataFrame({'X':
[1,None,3,4,5],
'Y': [1, None, 3,None,5],
'Z': [3,4,5,6,3]})
print("DataFrame:")
print(df)
sums=df.sum()
print("Column-wise Sum:")
print(sums)
Output:
DataFrame:
X Y Z
0 1.0 1.0 3
1 NaN NaN 4
2 3.0 3.0 5
3 4.0 NaN 6
4 5.0 5.0 3
Column-wise Sum:
X 13.0
Y 9.0
Z 21.0
dtype: float64
If you set skipna=True
, if the DataFrame has NaN
values, you will get NaN
the sum of the values.
import pandas as pd
df = pd.DataFrame({'X':
[1,None,3,4,5],
'Y': [1, None, 3,None,5],
'Z': [3,4,5,6,3]})
print("DataFrame:")
print(df)
sums=df.sum(skipna=False)
print("Column-wise Sum:")
print(sums)
Output:
DataFrame:
X Y Z
0 1.0 1.0 3
1 NaN NaN 4
2 3.0 3.0 5
3 4.0 NaN 6
4 5.0 5.0 3
Column-wise Sum:
X NaN
Y NaN
Z 21.0
dtype: float64
Here, you get the value of the sum X
of columns Y
and NaN
, since they both have NaN
values.
Sample code: DataFrame.sum()
Set in the methodmin_count
import pandas as pd
df = pd.DataFrame({'X':
[1,None,3,4,5],
'Y': [1, None, 3,None,5],
'Z': [3,4,5,6,3]})
print("DataFrame:")
print(df)
sums=df.sum(min_count=4)
print("Column-wise Sum:")
print(sums)
Output:
DataFrame:
X Y Z
0 1.0 1.0 3
1 NaN NaN 4
2 3.0 3.0 5
3 4.0 NaN 6
4 5.0 5.0 3
Column-wise Sum:
X 13.0
Y NaN
Z 21.0
dtype: float64
Here, you can get the value Y
of the sum of the column NaN
because Y
the column has only 3 non- NaN
values, which is less than min_count
the value of the parameter.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:107 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa
Pandas DataFrame DataFrame.mean() function
Publish Date:2025/04/30 Views:85 Category:Python
-
Python Pandas DataFrame.mean() function calculates the mean of the values of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No
Pandas DataFrame DataFrame.isin() function
Publish Date:2025/04/30 Views:133 Category:Python
-
The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains values the value specified in the input . pandas.DataFrame.isin(values) grammar DataFrame . isin(values) parameter values iterable - lis
Pandas DataFrame DataFrame.groupby() function
Publish Date:2025/04/30 Views:161 Category:Python
-
pandas.DataFrame.groupby() takes a DataFrame as input and divides the DataFrame into groups based on a given criterion. We can use groupby() the method to easily process large datasets. pandas.DataFrame.groupby() grammar DataFrame . groupby
Pandas DataFrame DataFrame.fillna() function
Publish Date:2025/04/30 Views:60 Category:Python
-
The pandas.DataFrame.fillna() function replaces the values DataFrame in NaN with a certain value. pandas.DataFrame.fillna() grammar DataFrame . fillna( value = None , method = None , axis = None , inplace = False , limit = None , down
Pandas DataFrame DataFrame.dropna() function
Publish Date:2025/04/30 Views:181 Category:Python
-
The pandas.DataFrame.dropna() function removes null values (missing values) from a DataFrame by dropping rows or columns that contain null values DataFrame . NaN ( Not a Number ) and NaT ( Not a Time ) represent null values. DataFrame
Pandas DataFrame DataFrame.assign() function
Publish Date:2025/04/30 Views:55 Category:Python
-
Python Pandas DataFrame.assign() function assigns new columns to DataFrame . pandas.DataFrame.assign() grammar DataFrame . assign( ** kwargs) parameter **kwargs Keyword arguments, DataFrame the column names to be assigned to are passed as k
Pandas DataFrame DataFrame.transform() function
Publish Date:2025/04/30 Views:120 Category:Python
-
Python Pandas DataFrame.transform() DataFrame applies a function on and transforms DataFrame . The function to be applied is passed as an argument to the function. The axis length of transform() the transformed should be the same as the ori