Reputation: 324
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
Upvotes: 1
Views: 1525
Reputation: 323226
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
Upvotes: 3