[英]create new column in pandas raises AttributeError: (“'str' object has no attribute 'str'”, 'occurred at index 0')
I have a data frame that looks the following:我有一个如下所示的数据框:
variable value
0 TrafficIntensity_end 217.0
1 TrafficIntensity_end+105 213.0
2 TrafficIntensity_end+120 204.0
3 TrafficIntensity_end+15 489.0
4 TrafficIntensity_end+30 479.0
5 TrafficIntensity_end+45 453.0
6 TrafficIntensity_end+60 387.0
7 TrafficIntensity_end+75 303.0
8 TrafficIntensity_end+90 221.0
9 pred_rf_end+15 545.0
10 pred_rf_end 244.0
11 pred_rf_end+30 448.0
12 pred_rf_end+45 408.0
13 pred_rf_end+60 363.0
14 pred_rf_end+75 305.0
15 pred_rf_end+90 199.0
16 pred_rf_end+105 181.0
17 pred_rf_end+120 163.0
I want to create a new column based on what the string in ['variable']
column contains.我想根据
['variable']
列中的字符串包含的内容创建一个新列。 I have the following code:我有以下代码:
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)
However this gives me the following error:但是,这给了我以下错误:
AttributeError: ("'str' object has no attribute 'str'", 'occurred at index 0')
Why does this happen and hw can I fix it?为什么会发生这种情况,我该如何解决? Thanks!
谢谢!
Use numpy.select
:使用
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'])
Your solution with test scalar by in
statement:您
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.