【问题标题】:How to Fill Numeric missing Values In Pandas Based On Another Column如何根据另一列填充 Pandas 中的数字缺失值
【发布时间】:2020-06-11 06:15:06
【问题描述】:

这里我需要在 pandas 中输入数值列

样本数据:

Age   Time_of_service

42       4

24       5

nan      27

26       4

31       5

54       21

21       2

Nan      32

45       18

19       0

65      35

nan       3

此处 Age 和 Time_of_Service 列高度相关。 根据以下条件,我需要估算缺失值

Time_of_Service >30
age = 60
Time_of_Service in (20,30)
age = 45
Time_of_Service in (10,20)
age = 35
Time_of_Service in (0,10)
age = 25

如何使用 Python 根据上述条件估算缺失值?

【问题讨论】:

    标签: python pandas numpy dataframe missing-data


    【解决方案1】:

    使用cut 进行分箱,然后将输出转换为整数并将Age 列中的缺失值替换为Series.fillna

    bins = [0,10,20,30,np.inf]
    labels = [25,35,45,60]
    new = pd.cut(df['Time_of_service'], bins=bins, labels=labels, include_lowest=True)
    df['Age'] = df['Age'].fillna(new.astype(int))
    print (df)
    
         Age  Time_of_service
    0   42.0                4
    1   24.0                5
    2   45.0               27
    3   26.0                4
    4   31.0                5
    5   54.0               21
    6   21.0                2
    7   60.0               32
    8   45.0               18
    9   19.0                0
    10  65.0               35
    11  25.0                3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多