Reputation: 1393
I have a pandas dataframe like this:
aa bb cc dd ee
a a b b foo
a b a a foo
b nan a a bar
b b b b bar
I want to create a new column df['ff']
like:
aa bb cc dd ee ff
a a b b foo c
a b a a foo c
a nan a a bar d
a b b b bar c
The logic is:
if df['bb'] is not null and df['aa']==a, then c else d
Based on answers to other questions, I think the answer should be something like this:
df['ff'] = df.apply(lambda x: x['bb'].isnull(),axis=1) & (x['aa']=='a')
But I get an error like this:
("'str' object has no attribute 'isnull'", 'occurred at index 0')
Upvotes: 1
Views: 1036
Reputation: 210972
I'd use the following vectorized approach:
In [47]: df['ff'] = np.where(df['bb'].notnull() & df['aa'].eq('a'), 'c', 'd')
In [48]: df
Out[48]:
aa bb cc dd ee ff
0 a a b b foo c
1 a b a a foo c
2 b NaN a a bar d
3 b b b b bar d
Upvotes: 2