0

I would like to replace NaN values in Target with the corresponding Node value. 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.

1
  • 1
    df["Target"] = df["Target"].fillna(df["Node"]) Commented Apr 9, 2021 at 20:54

1 Answer 1

1

Yes, df.fillna(value, ...) will allow the value (replacement) arg to be a Series (column), not just a constant:

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.


  1. Alternative, if df.fillna() hadn't allowed us to do this:

You could also use df.where() on your column of interest, where the df.where(... other) arg is your replacement column, 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.

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+') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.