1

数据框中的case语句,我在这里做错了什么?

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

我看到的错误:

3 if x =='以非货币救济关闭' ^ SyntaxError: invalid syntax

4

2 回答 2

1

我认为这是Series.map字典的最佳用途:

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)

如果某些值不匹配得到缺失值,您可以将其替换为原始值:

df1['Response'] = df1['Response'].map(d).fillna(df1['Response'])

或者通过另一个值,例如-1,然后也将值转换为整数,因为至少有一个NaN创建floats 列:

df1['Response'] = df1['Response'].map(d).fillna(-1).astype(int)
于 2019-11-29T07:04:21.923 回答
1

试试这种格式,它应该可以工作:

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 )
于 2019-11-29T07:41:31.620 回答