[英]Replacing NaN values in a column from a second column
I would like to replace NaN
values in Target
with the corresponding Node
value.我想用相应的
Node
值替换Target
中的NaN
值。 My data is:我的数据是:
Node Target Color
node1 node7 Red
node1 node9 Red
node3 node5 Green
node1 node3 Red
node3 node1 Red
node5 NaN Yellow
I would need to have:我需要:
Node Target Color
node1 node7 Red
node1 node9 Red
node3 node5 Green
node1 node3 Red
node3 node1 Red
node5 node5 Yellow # here the replacement
I think that a possible solution could be using an if statement to check if a node has Target
equal to NaN: if yes, then it would be possible to assign itself as target.我认为一个可能的解决方案可能是使用 if 语句来检查节点的
Target
是否等于 NaN:如果是,则可以将自己分配为目标。
Yes, df.fillna(value, ...)
will allow the value
(replacement) arg to be a Series (column) , not just a constant:是的,
df.fillna(value, ...)
将允许value
(replacement) arg 成为 Series (column) ,而不仅仅是一个常量:
df['Target'] = df['Target'].fillna(df['Node'])
Note this is better than if...else logic because it does one vectorized assignment to the entire dataframe, as the testcase below shows.请注意,这比 if...else 逻辑要好,因为它对整个 dataframe 进行了一个矢量化分配,如下面的测试用例所示。
df.fillna()
hadn't allowed us to do this:df.fillna()
不允许我们这样做: You could also use df.where()
on your column of interest, where the df.where(... other)
arg is your replacement column, df['Node']
:您还可以在您感兴趣的列上使用
df.where()
,其中df.where(... other)
arg 是您的替换列df['Node']
:
df['Target'] = df['Target'].where(df['Target'].notna(), df['Node'])
Note also how we use logical indexing, using df['Target'].notna()
to get us a mask/ logical vector of rows where Target is/isn't NA.还要注意我们如何使用逻辑索引,使用
df['Target'].notna()
来获取目标为/不是 NA 的行的掩码/逻辑向量。
Better testcase:更好的测试用例:
import pandas as pd
from io import StringIO
df = """ Node Target Color
node1 node7 Red
node1 node9 Red
node2 NaN Brown
node3 node5 Green
node1 node3 Red
node3 node1 Red
node5 NaN Yellow"""
df = pd.read_csv(StringIO(df), sep=r'\s+')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.