如果条件不符合预期的情况下填充列 [英] Populating column with if condition not working as expected
问题描述
让我们说我有以下DataFrame:
Lets say I have the following DataFrame:
0 1 2
1 10/1/2016 'stringvalue' 456
2 NaN 'anothersting' NaN
3 NaN 'and another ' NaN
4 11/1/2016 'more strings' 943
5 NaN 'stringstring' NaN
我想基于条件创建一个新列"Full Entry". 如果df [2]的值为NaN,则df ['Full Entry']也应为NaN.
I want to create a new column 'Full Entry' that is based on a condition. If the value of df[2] is NaN df['Full Entry'] should be NaN as well.
如果df [2]!= NaN df ['Full Entry']应该采用df [1]的值. 我想对每一行重复一次.
If df[2] != NaN df['Full Entry'] should take the value of df[1]. I want to repeat this for each row.
我想出了以下代码
df['Full_Entry'] = [df[1] if pd.isnull(x) == False else np.NaN for x in df[2]]
但这给了我以下结果
0 1 2 Full_Entry:
1 10/1/2016 'stringvalue' 456 0 stringv...
2 NaN 'anothersting' NaN NaN
3 NaN 'and another ' NaN NaN
4 11/1/2016 'more strings' 943 0 stringv...
5 NaN 'stringstring' NaN NaN
但是我想要的是这个
0 1 2 Full_Entry:
1 10/1/2016 'stringvalue' 456 stringvalue
2 NaN 'anothersting' NaN NaN
3 NaN 'and another ' NaN NaN
4 11/1/2016 'more strings' 943 more strings
5 NaN 'stringstring' NaN NaN
我的代码中的"if"条件似乎在正确的时刻触发,但仅使用第一行的值.由于某种原因,还包括了"0".
The 'if' condition in my code seems to trigger at the right moments, but only uses the value of the first row. And for some reason a '0' is included as well.
有人知道我的代码有什么问题吗?
Does anyone have an idea what is wrong with my code?
推荐答案
使用 df['Full_Entry']=np.where(pd.isnull(df.2), np.NaN, df.1)
这篇关于如果条件不符合预期的情况下填充列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!