Row-by-row fillna with respect to a specific column?

旧巷老猫 提交于 2019-12-04 16:55:37

Using the fillna function:

df.fillna(axis=1, method='backfill')

will do if there are no NaN's in the other columns.
If there are and you want to leave them untouched, I think the only option in this way is to perform the fillna on a subset of your dataframe. With example dataframe:

In [45]: df
Out[45]: 
     A    B    C    D   E   F
0  158  158  158  177   1  10
1  158  158  158  177   2  20
2  NaN  NaN  NaN  177   3  30
3  158  158  158  177 NaN  40
4  NaN  NaN  NaN  177   5  50

In [48]: df[['A', 'B', 'C', 'D']] = df[['A', 'B', 'C', 'D']].fillna(axis=1, method='backfill')

In [49]: df
Out[49]: 
     A    B    C    D   E   F
0  158  158  158  177   1  10
1  158  158  158  177   2  20
2  177  177  177  177   3  30
3  158  158  158  177 NaN  40
4  177  177  177  177   5  50

Udate: If you don't want to depend on the column order, you can also specify the values to use to fill for each row (like .fillna(value=df['D']). The only problem is that this only works for Series (when it is a dataframe, it tries to map the different values to fill to the different columns, not the rows). So with an apply to do it column by column, it works:

In [60]: df[['A', 'B', 'C']].apply(lambda x: x.fillna(value=df['D']))
Out[60]: 
     A    B    C
0  158  158  158
1  158  158  158
2  177  177  177
3  158  158  158
4  177  177  177
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!