4

Looking for an solution for if else statement in Pandas. Example:

col1  col2
1     NAN
2     NAN
3     NAN
NaN   01-2019
2     NAN

My new column needs to be col3;

  • When col1 = 1 or higher, add "text a"
  • When col1 = empty and col2 = an value, Take value of col2
  • Else; set value "text b"

I have now only; when col1 is higher then 1, set text a, otherwise text b.

df['col3'] = np.where(df['col1']>=1, 'text a', 'text b')

But missing the part where to check if col1 is empty and col2 has an value. To put that value inside col3.

How can i achieve that?

Thanks!

-- EDIT --

Asked also under answer for when col1 = 0 and col2 has an value, to set col3 to the value of col2.

so also:

col1  col2
0     01-2019

2 Answers 2

3

Use numpy.select with test missing and not missing values by Series.isna and Series.notna:

print (df)
   col1     col2
0   0.0      NaN <-added row for test all coditions failed
1   1.0      NaN
2   2.0      NaN
3   3.0      NaN
4   NaN  01-2019
5   2.0      NaN

m1 = df['col1'] > =1
m2 = df['col1'].isna() & (df['col2'].notna())
#oldier pandas versions
#m2 = df['col1'].isnull() & (df['col2'].notnull())
df['col3'] = np.select([m1, m2], ['text a', df['col2']], 'text b')
print (df)
   col1     col2     col3
0   0.0      NaN   text b
1   1.0      NaN   text a
2   2.0      NaN   text a
3   3.0      NaN   text a
4   NaN  01-2019  01-2019
5   2.0      NaN   text a

Another solution with double np.where:

df['col3'] = np.where(m1, 'text a',
             np.where(m2, df['col2'], 'text b'))

EDIT:

Condition is changed:

m2 = (df['col1'] == 0) & (df['col2'].notna())
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Thanks. Can you change the answer to have also in mind when col1=0, and col2 has value, that it will take the value in col3?
Hi there. The supplier has modified there data, and now in the stock there can also be negative values. for example in col1, -10. How can we achieve an solution that -10 also gets the col2 date? <=0 doesn't work btw.
1

An another way to do it apart from @jexrael awesome answer is to use apply

In [27]: def condition(r):
    ...:     if r['col1'] >= 1: return "text a"
    ...:     if pd.isnull(r['col1']) and pd.notnull(r['col2']): return r['col2']
    ...:     return "text b"
    ...:

In [28]: df['col3'] = df.apply(condition, axis=1)

In [29]: df
Out[29]:
   col1     col2     col3
0   1.0      NaN   text a
1   2.0      NaN   text a
2   3.0      NaN   text a
3   NaN  01-2019  01-2019
4   2.0      NaN   text a

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.