所以这应该是世界上最简单的事情。伪代码:
Replace column C with NaN if column E is NaN
我知道我可以通过拉出列E为NaN的所有数据帧行来替换所有C列,然后将其合并到原始数据集上,但这对于简单的操作来说似乎很多工作。为什么这不起作用:
示例数据:
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)
如何在一行中做到这一点?
答案 0 :(得分:5)
使用BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line=stdIn.readLine()) !=null && line.length()!=0){
}
:
np.where
或者只是掩饰df:
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