简体   繁体   English

分别替换pandas.dataframe中的NaN

[英]Individually replace NaN in pandas.dataframe

I have a 900 x 7 dataframe in which 3 fields contain some NaN values. 我有一个900 x 7数据框,其中3个字段包含一些NaN值。

Instead of simply replacing these values with the some feature average, I have created a function to use an algorithm to estimate the likely value of each NaN based on the other values in that row. 我创建了一个函数,使用一种算法根据该行中的其他值来估计每个NaN的可能值,而不是简单地用某个特征平均值替换这些值。

How can I iterate over each NaN to change it's value using my custom function? 如何使用自定义函数遍历每个NaN来更改其值?

My function takes the row ID, the other feature names, and the feature containing the NaN as arguments. 我的函数使用行ID,其他要素名称以及包含NaN的要素作为参数。

Eg 例如

custom_fillnan(id=0, ins=["val0", "val1", "val2"], out="valn")

Example dataframe: 示例数据框:

ID    val0    val1    val2    ...    valn
0      1        2       3     ...    NaN
1      1      NaN       3     ...     4
2      0        0     NaN     ...     1
...

IIUC you could use apply with axis=1 and fillna with your custom function: 您可以apply IIUC与axis=1一起apply ,并将fillna与自定义函数一起使用:

In [80]: df
Out[80]: 
   ID  val0  val1  val2  valn
0   0     1     2     3   NaN
1   1     1   NaN     3     4
2   2     0     0   NaN     1


In [83]: df.apply(lambda x: x.fillna(pd.np.mean(x.iloc[1:])), axis=1)
Out[83]: 
   ID  val0      val1      val2  valn
0   0     1  2.000000  3.000000     2
1   1     1  2.666667  3.000000     4
2   2     0  0.000000  0.333333     1

Instead of pd.np.mean you could use your function. 可以使用函数代替pd.np.mean x.iloc[1:] is used because, as I understand, you want to use for your function only val columns. x.iloc[1:]是因为据我了解,您只想将val列用于函数。

EDIT 编辑

If you want to get column names for missing values you could apply or use that function for processing: 如果要获取缺少值的列名,可以应用或使用该函数进行处理:

def func(x):
    x.loc[x.isnull()] = x.index[x.isnull()]
    return x

In [209]: df.apply(func, axis=1)
Out[209]: 
   ID  val0  val1  val2  valn
0   0     1     2     3  valn
1   1     1  val1     3     4
2   2     0     0  val2     1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM