我有一个如下所示的数据框: variable value0 TrafficIntensity_end 217.01 TrafficIntensity_end+105 213.02 TrafficIntensity_end+120 204.03 TrafficIntensity_end+15 489.04 TrafficIntensity_end+30 479.05 TrafficIntensity_end+45 453.06 TrafficIntensity_end+60 387.07 TrafficIntensity_end+75 303.08 TrafficIntensity_end+90 221.09 pred_rf_end+15 545.010 pred_rf_end 244.011 pred_rf_end+30 448.012 pred_rf_end+45 408.013 pred_rf_end+60 363.014 pred_rf_end+75 305.015 pred_rf_end+90 199.016 pred_rf_end+105 181.017 pred_rf_end+120 163.0我想根据['variable']列中的字符串包含的内容创建一个新列。我有以下代码:def classify(row): if row['variable'].str.contains('TrafficIntensity'): return 'Real Traffic Intensity' elif row['variable'].str.contains('pred_rf_end'): return 'Predicited Value'a['category'] = a.apply(classify, axis=1)但是,这给了我以下错误:AttributeError: ("'str' object has no attribute 'str'", 'occurred at index 0')为什么会发生这种情况,我该如何解决?谢谢!
1 回答
largeQ
TA贡献2039条经验 获得超7个赞
使用numpy.select
:
m1 = df['variable'].str.contains('TrafficIntensity')
m2 = df['variable'].str.contains('pred_rf_end')
a['category'] = np.select([m1, m2],
['Real Traffic Intensity','Predicited Value'],
a['variable'])
您通过in语句测试标量的解决方案:
def classify(x):
if 'TrafficIntensity' in x:
return 'Real Traffic Intensity'
elif 'pred_rf_end' in x:
return 'Predicited Value'
else:
return x
a['category'] = a['variable'].apply(classify)
添加回答
举报
0/150
提交
取消