假设我有这张 table

Type | Killed | Survived
Dog      5         2
Dog      3         4
Cat      1         7
Dog     nan        3
cow     nan        2
Killed 上的 [Type] = Dog 值之一缺失。

我想为 [Killed] 估算 [Type] = Dog 中的平均值。

我的代码如下:
  • 搜索均值
  • df[df['Type'] == 'Dog'].mean().round()
    这会给我平均值(大约 2.25)
  • 估算平均值(这是问题开始的地方)
  • df.loc[(df['Type'] == 'Dog') & (df['Killed'])].fillna(2.25, inplace = True)
    代码运行,但该值未插补,NaN 值仍然存在。

    我的问题是,如何根据 [Killed] 估算 [Type] = Dog 中的平均值。

    最佳答案

    对我来说工作:

    df.ix[df['Type'] == 'Dog', 'Killed'] = df.ix[df['Type'] == 'Dog', 'Killed'].fillna(2.25)
    print (df)
      Type  Killed  Survived
    0  Dog    5.00         2
    1  Dog    3.00         4
    2  Cat    1.00         7
    3  Dog    2.25         3
    4  cow     NaN         2
    

    如果需要 fillna by Series - 因为有 2 列 KilledSurvived :
    m = df[df['Type'] == 'Dog'].mean().round()
    print (m)
    Killed      4.0
    Survived    3.0
    dtype: float64
    
    df.ix[df['Type'] == 'Dog'] = df.ix[df['Type'] == 'Dog'].fillna(m)
    print (df)
      Type  Killed  Survived
    0  Dog     5.0         2
    1  Dog     3.0         4
    2  Cat     1.0         7
    3  Dog     4.0         3
    4  cow     NaN         2
    

    如果只需要在 Killed 列中填充:
    #if dont need rounding, omit it
    m = round(df.ix[df['Type'] == 'Dog', 'Killed'].mean())
    print (m)
    4
    
    df.ix[df['Type'] == 'Dog', 'Killed'] = df.ix[df['Type'] == 'Dog', 'Killed'].fillna(m)
    print (df)
      Type  Killed  Survived
    0  Dog     5.0         2
    1  Dog     3.0         8
    2  Cat     1.0         7
    3  Dog     4.0         3
    4  cow     NaN         2
    

    您可以重用以下代码:
    filtered = df.ix[df['Type'] == 'Dog', 'Killed']
    print (filtered)
    0    5.0
    1    3.0
    3    NaN
    Name: Killed, dtype: float64
    
    df.ix[df['Type'] == 'Dog', 'Killed'] = filtered.fillna(filtered.mean())
    print (df)
      Type  Killed  Survived
    0  Dog     5.0         2
    1  Dog     3.0         8
    2  Cat     1.0         7
    3  Dog     4.0         3
    4  cow     NaN         2
    

    关于python - 基于特定列属性的 Pandas fillna(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39242615/

    10-11 03:53