The Python Oracle

If condition with a dataframe

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: A Thousand Exotic Places Looping v001

--

Chapters
00:00 If Condition With A Dataframe
00:50 Answer 1 Score 4
01:13 Accepted Answer Score 5
01:23 Answer 3 Score 2
01:34 Answer 4 Score 2
01:43 Thank you

--

Full question
https://stackoverflow.com/questions/6984...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe

#avk47



ACCEPTED ANSWER

Score 5


You can use where:

df['score'] = (df['tg']*5).where(df['tg'].between(10, 32), df['tg']/5)



ANSWER 2

Score 4


Use np.where:

# do you need `inclusive=True`? Expected output says yes, your logic says no
mask = df['tg'].between(10,32, inclusive=False)
df['score'] = df['tg'] * np.where(mask, 5, 1/2)

 # or
 # df['score'] = np.where(mask, df['tg'] * 5, df['tg']/2)

Output:

   year  day  month  tg  rain  score
0  2001    1      1  10     1    5.0
1  2001    2      1  11     2   55.0
2  2001    3      1  12     3   60.0
3  2001    4      1  13     2   65.0
4  2001    1      2  50     4   25.0
5  2001    2      2  21     1  105.0
6  2001    3      2  -1     2   -0.5
7  2001    4      2  23     1  115.0



ANSWER 3

Score 2


Let try to fix it with for loop

[x * 5 if (x > 10 and x < 32) else (x / 2) for x in df['tg']]
Out[64]: [5.0, 55, 60, 65, 25.0, 105, -0.5, 115]



ANSWER 4

Score 2


You can use df.loc

mask = (df["tg"] > 10) & (df["tg"] < 32)
df.loc[mask, "score"] = df["tg"] * 5
df.loc[~mask, "score"] = df["tg"] / 2