亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在熊貓中創建新列會引發 AttributeError:

在熊貓中創建新列會引發 AttributeError:

楊__羊羊 2022-12-06 16:46:26
我有一個如下所示的數據框:                  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條經驗 獲得超8個贊

使用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)


查看完整回答
反對 回復 2022-12-06
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號