【问题标题】:Filling missing values with mode of column relative to another column in Python用相对于Python中另一列的列模式填充缺失值
【发布时间】:2018-04-07 02:26:05
【问题描述】:

我有一个数据框,其中包含 2 列 City_Type 和 Vehicle_Type 以及许多其他列。现在我在 Vehicle_Type 列中有一些缺失值,我需要根据 City_Type 使用 Vehicle_Type 的模式进行估算。 首先,我需要编写一个 python 程序来获得以下输出。然后我需要根据以下输出填充 Vehicle_Type 字段中的缺失值。

City_Type    Vehicle_Type
Tier1        Sedans
Tier2        Hatchback
Tier3        SUV

【问题讨论】:

    标签: python pandas scipy analytics pandas-groupby


    【解决方案1】:

    你可以在模式下使用 groupby 和 fillna

    df.groupby('City_Type').Vehicle_Type.transform(lambda x: x.fillna(x.mode()[0]))
    

    【讨论】:

    • 它不起作用。也许我的问题需要更清楚。
    • 它不起作用。也许我的问题需要更清楚。示例数据:Tier1 Sedan Tier2 SUV Tier3 Hatchback Tier1 Sedan Tier2 SUV Tier3 Hatchback Tier1 Tier2 Hatchback Tier3 Tier1 Sedan Tier2 Tier3 Sedan Tier1 Hatchback Tier2 SUV Tier3 SUV 我需要使用与 City_Type 相关的模式填充缺失值
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2017-02-08
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2021-09-05
    相关资源
    最近更新 更多