如果另一列是NaN,如何替换列中的值? [英] How to replace values in a column if another column is a NaN?
本文介绍了如果另一列是NaN,如何替换列中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,这应该是地球上最简单的事情.伪代码:
So this should be the easiest thing on earth. Pseudocode:
Replace column C with NaN if column E is NaN
我知道我可以通过以下方式来做到这一点:拉出E列为NaN的所有数据帧行,替换所有C列,然后将其合并到原始数据集中,但这对于一个简单的操作来说似乎需要做很多工作.为什么这样行不通:
I know I can do this by pulling out all dataframe rows where column E is NaN, replacing all of Column C, and then merging that on the original dataset, but that seems like a lot of work for a simple operation. Why doesn't this work:
样本数据:
dfz = pd.DataFrame({'A' : [1,0,0,1,0,0],
'B' : [1,0,0,1,0,1],
'C' : [1,0,0,1,3,1],
'D' : [1,0,0,1,0,0],
'E' : [22.0,15.0,None,10.,None,557.0]})
替换功能:
def NaNfunc(dfz):
if dfz['E'] == None:
return None
else:
return dfz['C']
dfz['C'] = dfz.apply(NaNfunc, axis=1)
以及如何在一行中做到这一点?
And how to do this in one line?
推荐答案
使用np.where
:
In [34]:
dfz['C'] = np.where(dfz['E'].isnull(), dfz['E'], dfz['C'])
dfz
Out[34]:
A B C D E
0 1 1 1 1 22
1 0 0 0 0 15
2 0 0 NaN 0 NaN
3 1 1 1 1 10
4 0 0 NaN 0 NaN
5 0 1 1 0 557
或者只是掩盖df:
In [38]:
dfz.loc[dfz['E'].isnull(), 'C'] = dfz['E']
dfz
Out[38]:
A B C D E
0 1 1 1 1 22
1 0 0 0 0 15
2 0 0 NaN 0 NaN
3 1 1 1 1 10
4 0 0 NaN 0 NaN
5 0 1 1 0 557
这篇关于如果另一列是NaN,如何替换列中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文