关于特定列的逐行填充? [英] Row-by-row fillna with respect to a specific column?
问题描述
我有以下pandas数据框,我想用D列中的值以行的方式填充AC列中的NaN.是否有明确的方法来执行此操作,因此我可以定义所有NaN都应依赖D列中的值是否按行排列?我找不到在fillna()中显式执行此操作的方法.
I have the following pandas dataframe and I would like to fill the NaNs in columns A-C in a row-wise fashion with values from columns D. Is there an explicit way to do this where I can define that all the NaNs should depend row-wise on values in column D? I couldn't find a way to explicitly do this in fillna().
请注意,还有另外的列E-Z ,它们具有自己的NaN,并且可能具有其他填写NaN的规则,因此应保持不变.
Note that there are additional columns E-Z which have their own NaNs and may have other rules for filling in NaNs, and should be left untouched.
A B C D E
158 158 158 177 ...
158 158 158 177 ...
NaN NaN NaN 177 ...
158 158 158 177 ...
NaN NaN NaN 177 ...
仅希望将其用于A-C列:
Would like to have this for columns A-C only:
A B C D E
158 158 158 177 ...
158 158 158 177 ...
177 177 177 177 ...
158 158 158 177 ...
177 177 177 177 ...
谢谢.
推荐答案
使用fillna
函数:
df.fillna(axis=1, method='backfill')
如果其他列中没有NaN,则
会执行.
如果有并且您希望不受影响,我认为这种方式的唯一选择是对数据帧的子集执行fillna
.带有示例数据框:
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
日期:如果不想依赖列顺序,还可以指定用于填充每一行的值(例如.fillna(value=df['D']
).唯一的问题是,这仅适用于Series(当它是一个数据框时,它将尝试映射不同的值以填充到不同的列,而不是行).因此,通过逐列应用,它可以工作:
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
这篇关于关于特定列的逐行填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!