Sum across all NaNs in pandas returns zero

Let's understand that why dataframe.sum() method returns 0 where it finds all the nan values?
Submitted by Pranit Sharma, on November 24, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Suppose we are given the Pandas dataframe with multiple columns, each column has some integer value and some Nan values.

Summing across all NaNs in pandas returns zero

For calculating the sum of the dataframe along the rows if the sum method and counters all the Nan values, it returns 0 as the sum but the required result is the Nan value.

The dataframe.sum() method returns 0 where it finds all the nan values. The reason for this behavior of the sum() method is the incorrect value of the parameter or the missing parameter that is skipna. To return the Nan value when the sum method counters all the Nan values, we must need to set the parameter (skipna=false) while using this method.

Let us understand with the help of an example,

Python program to demonstrate why dataframe.sum() method returns 0 where it finds all the nan values

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating dataframe
df = pd.DataFrame({'A':[1,2,np.nan,3],'b':[np.nan,2,np.nan,3]})

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Calculating sum along the row without skipna
res = df.sum(axis=1)

# Display result
print("Result:\n",res,"\n")

# Using skipna=false
res = df.sum(axis=1,skipna=False)

# Display result
print("New Result:\n",res)

Output

The output of the above program is:

Example: Sum across all NaNs in pandas returns zero

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.