1

I have a pd Dataframe like this:

df = pd.DataFrame({'val':[0.1,0.2,0.3,None,None],'parent':[None,None,None,0,2]})

   parent  val
0     NaN  0.1
1     NaN  0.2
2     NaN  0.3
3     0.0  NaN
4     2.0  NaN

where parent represents an index within the pandas df. I want to create a new column that has either the value, or the value of the parent.

that would look like this:

   parent  val  val_full
0     NaN  0.1       0.1
1     NaN  0.2       0.2
2     NaN  0.3       0.3
3     0.0  NaN       0.1
4     2.0  NaN       0.3

This is a fairly large dataframe (10k+ rows), so something efficient would be preferable. How can I do this without using something like .iterrows()?

1 Answer 1

5

In your case do

df['new'] = df.val
df.loc[df.new.isna(),'new'] = df.loc[df.parent.dropna().values,'val'].values
df
Out[289]: 
   val  parent  new
0  0.1     NaN  0.1
1  0.2     NaN  0.2
2  0.3     NaN  0.3
3  NaN     0.0  0.1
4  NaN     2.0  0.3

Or try fillna with replace

df['new'] = df.val.fillna(df.parent.replace(df.val))
Out[290]: 
0    0.1
1    0.2
2    0.3
3    0.1
4    0.3
Name: val, dtype: float64
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.