0
# Check for None and NaN values in the dataframe and clean if needed
print("Any NaN values in the dataframe? True or False",  end='\n')
display(dfManual_With_NaN.isnull().values.any())    
print("",  end='\n')

Output: True

print("Total Number of NaN values in the dataframe",  end='\n')
display(dfManual_With_NaN.isnull().sum().sum())
print("",  end='\n')

Output: 1

print("Display the total number of rows with a NaN",  end='\n')
df_NaN_Rows = dfManual_With_NaN[dfManual_With_NaN.isnull().T.any().T]  
display(df_NaN_Rows.head(100))
print("",  end='\n')


Output:

在此处输入图像描述

# Update None and NaN to zero
dfManual_Booked = dfManual_With_NaN.fillna(value='NaN', inplace=True) # Replace None values with NaN
dfManual_Booked = dfManual_Booked.fillna(0)              # Replace NaN with 0. 

Errors here: 'NoneType' object has no attribute 'isnull'

所以我将 None 值更新为 NaN,然后​​将所有 NaN 设置为 0。关于为什么失败的任何指导?

4

1 回答 1

2

使用 时inplace=True,您是在同一个数据帧上执行操作,而不是返回一个新数据帧(函数调用也将返回Nonewhen inplace=True)。

并且在通话NaNNone也受到同样的对待fillna,所以只要做dfManual_Booked = dfManual_Booked.fillna(0)就足够了。(或只是dfManual_Booked.fillna(0, inplace=True)

于 2018-05-28T09:01:16.117 回答