我有一个包含 2 列 City_Type 和 Vehicle_Type 以及许多其他列的数据框。现在我在 Vehicle_Type 列中有一些缺失值,我需要根据 City_Type 使用 Vehicle_Type 模式进行插补。 首先我需要编写一个 python 程序来获得以下输出。然后我需要根据以下输出填充 Vehicle_Type 字段中缺失的值。
City_Type Vehicle_Type
Tier1 Sedans
Tier2 Hatchback
Tier3 SUV
最佳答案
您可以将 groupby 和 fillna 与模式一起使用
df.groupby('City_Type').Vehicle_Type.transform(lambda x: x.fillna(x.mode()[0]))
关于python - 在Python中用相对于另一列的列的模式填充缺失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46941874/