代码之家  ›  专栏  ›  技术社区  ›  asn

通过比较列,用另一个数据帧的列中的值填充NAN

  •  0
  • asn  · 技术社区  · 8 年前

    NaNs 在里面 Age 包含来自数据帧的值的列 df 它还有一些公共字段作为我的原始数据帧 Orig .

    我只想 Sex, Pclass,Title

    df的内容有:

      Sex Pclass Title  Survived       Age      Fare
    0   0   1   Miss    0.958333    29.744681   121.694356
    1   0   1   Mrs     0.976744    40.400000   92.615505
    2   0   1   Officer 1.000000    49.000000   25.929200
    3   0   1   Royalty 1.000000    40.500000   63.050000
    4   0   2   Miss    0.941176    22.390625   20.393750
    5   0   2   Mrs     0.904762    33.547619   23.246231
    6   0   3   Miss    0.500000    16.123188   15.248043
    7   0   3   Mrs     0.500000    33.515152   18.233529
    8   1   1   Master  1.000000    5.306667    117.802767
    9   1   1   Mr      0.345794    41.580460   68.034385
    10  1   1   Officer 0.444444    51.125000   53.761111
    11  1   1   Royalty 0.333333    42.333333   28.216667
    12  1   2   Master  1.000000    2.258889    27.306022
    13  1   2   Mr      0.087912    32.768293   19.252564
    14  1   2   Officer 0.000000    42.000000   16.796875
    15  1   3   Master  0.392857    5.350833    28.177232
    16  1   3   Mr      0.112853    28.724891   11.299762
    

    鉴于 原始 是:

    enter image description here

    我该怎么填这个表 在里面 年龄 中的列 使用

    1 回复  |  直到 8 年前
        1
  •  2
  •   Vishnu Kunchur    8 年前
    创建感兴趣的列列表:
    cols = [
        *orig.columns.difference(df.columns),'Sex_x','Pclass_x',
        'Title_x', 'Survived_x', 'Age_x', 'Age_y', 'Fare'
    ]
    
    对列执行左合并(联接) Sex , PClass 和 Title NaN Age_y :
      orig = orig.merge(df, on=['Sex', 'PClass', 'Title'], how='left')[cols]
        orig['Age'] = df['Age_x'].fillna(df['Age_y'])
    
    删除现在多余的 Age_x 和 年龄
    orig.drop(columns=['Age_x', 'Age_y'], inplace=True)