python - 在Python中用相对于另一列的列的模式填充缺失值

标签 python pandas scipy analytics pandas-groupby

我有一个包含 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/

相关文章:

python - Django REST Framework - 在创建时序列化嵌套关系

python - 通过 Gmail API 发送 Excel 文件但附件已损坏

python - 正则表达式和 Pandas : extract partial string on name match

python - 稀疏矩阵模式上的 Scipy 边界条件

python - 迭代 pandas dataframe 的行并添加值

Python 不考虑字符串中的字符 "c"

python - 来自网络的 Pandas read_csv 在 Python 3.8 和 3.10 之间表现不同?

python - 更改 SHAP 图的纵横比

python - 用python进行傅立叶变换

python - 在不显式定义梯度的情况下优化 scipy 中的函数