Lambda 表达式的比较运算符无法找到 NaN 值

Comparison operator with Lambda Expression is not able to find NaN values

我正在尝试根据另一个 column.But 的分类值替换列中的空值 == 运算符让我后悔我生命中的所有重大决定。我在火车集中有 8523 行和 12 列,其中 7 列是分类的,5 列是数字的。

Columns are 'Item_Identifier', 'Item_Weight', 'Item_Fat_Content', 'Item_Visibility', 'Item_Type', 'Item_MRP', 'Outlet_Identifier', 'Outlet_Establishment_Year', 'Outlet_Size', 'Outlet_Location_Type', 'Outlet_Type', 'Item_Outlet_Sales'

我想根据 'Outlet_Location_Type' 的分类值在 'Item_Weight' 列中填充 NaN 值(float dtype)。我有一个字典(city_type_mean),其中分类值作为键,相应的值被替换为值。 我使用了以下代码

train["Item_Weight"] = train.apply(lambda x: city_type_mean[x['Outlet_Location_Type']] if x["Item_Weight"] == np.nan else x["Item_Weight"], axis=1) 

但 Nan 值不受影响。我在有问题的代码图像之后附加了一个火车数据样本。 . 到目前为止,我解决的问题是上面的 if 条件总是计算为 false 导致执行 else。我已经用 is 和 pd.isnull() 方法尝试了条件,但是没有 avail.Any 对问题的帮助很多 appreciated.Also 请在标记这个问题之前告诉我,以防重复。

你能用 isnan 代替 == np.nan 吗?

train["Item_Weight"] = train.apply(lambda x: city_type_mean[x['Outlet_Location_Type']] if  np.isnan(x["Item_Weight"]) else x["Item_Weight"], axis=1)