简体   繁体   English

从第二列替换列中的 NaN 值

[英]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 进行了一个矢量化分配,如下面的测试用例所示。


  1. Alternative, if 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.

相关问题 用另一列中的正则表达式替换一列中的 NaN 值 - Replacing NaN values in one column with regex from another column 通过将一列中的 NaN 值替换为另一列中的非 NaN 值来更新 Pandas dataframe - Updating a Pandas dataframe by replacing NaN values in a column with not NaN values from another column 根据另一列的值用 nan 替换值 - Replacing values with nan based on values of another column 迭代Pandas Column并替换nan值 - Iterating through Pandas Column and replacing nan values Pandas 用 nan 替换所有列值 - Pandas replacing all column values with nan Python:在匹配不同列中的值后,用来自另一个数据帧的值替换特定列中的 NaN - Python: Replacing NaN in a specific column by values from another dataframe after matching values in a different column 将列中的 NaN 值替换为该列中特定类别的模式 - Replacing NaN values in a column with the mode of a particular Category in that the column 使用基于(非唯一)列值的其他行中的值替换 DataFrame 行中的 NaN 值 - Replacing NaN values in a DataFrame row with values from other rows based on a (non-unique) column value 应用循环,同时用另一列中的值替换 dataframe 中的 NaN 值 - Apply for loop while replacing NaN values in a dataframe with values from another column 连接 Pandas DataFrame 中的列值,用逗号替换“NaN”值 - Concatenate column values in Pandas DataFrame replacing “NaN” values with comma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM