1

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')

1 Answer 1

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.