1

I have a dataframe like this:

x,y,z 1,2,3 2,3,4 1,6,7 

And I have a column like this:

a 4 5 

Column a should be added when x = 1. So it should look like this:

x, y, z, a 1 2 3 4 2 3 4 NaN 1 6 7 5 

How would I do this using pandas

1 Answer 1

3

Assign pandas object index will be one of the hidden key , so we should do assign by value only

df.loc[df['x'] == 1, 'a'] = col['a'].values 
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.