熊猫用条件填充了另一列中的nan值?
我有以下数据框:
data = {'feature1_in_use?': [0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1],
'feature1_available?': [0, 1, 1, 'NA', 1, 'NA', 1, 1, 'NA', 1, 1]}
df = pd.DataFrame(data)
在“ feature1_available?”列中我想用'feature1_in_use中的值填充NAN? '仅在1时,否则用'x'或任何有效的字符串填充它。
看起来应该这样:
data = {'feature1_in_use?': [0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1],
'feature1_available?': [0, 1, 1, 'X', 1, 1, 1, 1, 1, 1, 1]}
df = pd.DataFrame(data)
I have the following dataframe:
data = {'feature1_in_use?': [0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1],
'feature1_available?': [0, 1, 1, 'NA', 1, 'NA', 1, 1, 'NA', 1, 1]}
df = pd.DataFrame(data)
In the column 'feature1_available?' I want to fill the nan with the values from 'feature1_in_use? ' only when these are 1, otherwise fill it up with 'X' or any valid string.
It should look like this:
data = {'feature1_in_use?': [0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1],
'feature1_available?': [0, 1, 1, 'X', 1, 1, 1, 1, 1, 1, 1]}
df = pd.DataFrame(data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用的灵活选项是
np.Select()
A flexible option you could use would be
np.select()
假设您希望您的空白值为“ x”,然后执行以下操作:
步骤1填充 farture1_available 的空白值来自 features1_in_use 。步骤2用“ x”填充所有剩余的NAN值(或您选择的任何其他值
Lets say you want your blank value to be 'x' then do the following:
Step 1 fills blank values in feature1_available from feature1_in_use. Step 2 fills any remaining nan values with 'x' (or any other value you choose
第一行创建了一个系列,其中除1以外的其他所有内容都是'x'。然后,“ na”被
np.nan
替换,该使我们能够用创建的输出的系列填充缺失值:
The first line creates a series where everything other than 1 is 'X'. Then 'NA' is replaced with
np.NaN
which allows us to fill in the missing values with the series that was createdOutput: