简体   繁体   English

在 pandas 中创建新列引发 AttributeError:(“'str' object 没有属性 'str'”,'发生在索引 0')

[英]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.

相关问题 AttributeError: ("'str' 对象没有属性 'str'",'发生在索引 31978') - AttributeError: ("'str' object has no attribute 'str'", 'occurred at index 31978') AttributeError:(“'str' object 没有属性'包含'”,'发生在索引持续时间') - AttributeError: (“'str' object has no attribute 'contains'”, 'occurred at index duration') Pandas str.extract: AttributeError: 'str' 对象没有属性 'str' - Pandas str.extract: AttributeError: 'str' object has no attribute 'str' Pandas: AttributeError: 'str' object 没有属性 'isnull' - Pandas: AttributeError: 'str' object has no attribute 'isnull' Pandas AttributeError: 'str' object 没有属性 'loc' - Pandas AttributeError: 'str' object has no attribute 'loc' .endswith() 方法引发异常“AttributeError: 'str' object has no attribute 'value'” - .endswith() method raises exception "AttributeError: 'str' object has no attribute 'value'" 发生异常:AttributeError 'str' object has no attribute 'tk' - Exception has occurred: AttributeError 'str' object has no attribute 'tk' 在带有apply语句的pandas中使用str.contains会引发str对象没有属性str错误 - Use str.contains in pandas with apply statement raises str object has not attribute str error Python pandas的“索引”对象没有属性“ str” - Python pandas 'Index' object has no attribute 'str' AttributeError: 'str' 对象没有属性 'str' - AttributeError: 'str' object has no attribute 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM