Reputation: 3340
Case statement inside the data frame, what am I doing wrong here?
df1['Response'] = [4 if x =='Closed with monetary relief'
3 if x =='Closed with non-monetary relief'
2 if x =='Closed with explanation'
1 if x =='Closed'
0 if x =='Untimely response' for x in df1['Response']]
Error I see:
3 if x =='Closed with non-monetary relief' ^ SyntaxError: invalid syntax
Upvotes: 1
Views: 55
Reputation: 7222
Try this format, it should work:
df1.Response.apply(lambda x: 4 if x =='Closed with monetary relief' else
3 if x =='Closed with non-monetary relief' else
2 if x =='Closed with explanation' else
1 if x =='Closed' else
0 if x =='Untimely response' else None )
Upvotes: 1
Reputation: 863801
I think here is best use Series.map
by dictionary:
d = {'Closed with monetary relief' : 4,
'Closed with non-monetary relief':3.
'Closed with explanation':2,
'Closed':1,
'Untimely response':0}
df1['Response'] = df1['Response'].map(d)
If some value not match get missing value, you can replace it by original:
df1['Response'] = df1['Response'].map(d).fillna(df1['Response'])
Or by some another value e.g. -1
, then also convert values to integers, because at least one NaN
create float
s column:
df1['Response'] = df1['Response'].map(d).fillna(-1).astype(int)
Upvotes: 1