将数据框中的所有 None 和 NaN 值设置为 0 - 'NoneType' 对象没有属性 'isnull'
Set all None and NaN values to 0 in a datframe - 'NoneType' object has no attribute 'isnull'
# 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。关于失败原因的任何指导?
当使用 inplace=True
时,您是在同一个数据帧上执行操作而不是 return 一个新数据帧(函数调用也会 return None
当inplace=True
)。
此外,NaN
和 None
对于 fillna
调用的处理方式相同,因此只需 dfManual_Booked = dfManual_Booked.fillna(0)
就足够了。 (或者只是 dfManual_Booked.fillna(0, inplace=True)
)
# 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。关于失败原因的任何指导?
当使用 inplace=True
时,您是在同一个数据帧上执行操作而不是 return 一个新数据帧(函数调用也会 return None
当inplace=True
)。
此外,NaN
和 None
对于 fillna
调用的处理方式相同,因此只需 dfManual_Booked = dfManual_Booked.fillna(0)
就足够了。 (或者只是 dfManual_Booked.fillna(0, inplace=True)
)