Issue
What I want is to write df['good_day'] = 1
if
sunshine >= 8
rain <= 3
temperatur >= 15 and <= 25
and otherwise df['good_day'] = 0
d = {'sunshine': [8, 9, 6, 5],
'rain': [2, 4, 0, 8],
'temperatur': [22, 32, -12, 4],
'id': [1, 2, 3, 4]}
df = pd.DataFrame(data=d)
print(df)
sunshine rain temperatur id
0 8 2 22 1
1 9 4 32 2
2 6 0 -12 3
3 5 8 4 4
df['good_day'] = 0
df.loc[df['sunshine'] >= 8, 'good_day'] = 1
df.loc[df['rain'] <= 3, 'good_day'] = 1
df['good_day'] = df.where(df['temperatur'].between(15, 25), 1)
print(df)
sunshine rain temperatur id good_day
0 8 2 22 1 8
1 9 4 32 2 1
2 6 0 -12 3 1
3 5 8 4 4 1
What I want
sunshine rain temperatur id good_day
0 8 2 22 1 1
1 9 4 32 2 0
2 6 0 -12 3 0
3 5 8 4 4 0
Solution
Also you can use this:
df.loc[(df['temperatur'] <= 25) & (df['temperatur'] >= 15) & (df['rain'] <= 3) & ( df['sunshine'] >= 8), 'good_day'] = 1
Answered By - Sejuh_M
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.