假設我們在 R 中有以下代碼,它在 Python 中的等效 Pandas 資料框語法/方法是什么?
network_tickets <- contains(comcast_data$CustomerComplaint, match = 'network', ignore.case = T)
internet_tickets <- contains(comcast_data$CustomerComplaint, match = 'internet', ignore.case = T)
billing_tickets <- contains(comcast_data$CustomerComplaint, match = 'bill', ignore.case = T)
email_tickets <- contains(comcast_data$CustomerComplaint, match = 'email', ignore.case = T)
charges_ticket <- contains(comcast_data$CustomerComplaint, match = 'charge', ignore.case = T)
comcast_data$ComplaintType[internet_tickets] <- "Internet"
comcast_data$ComplaintType[network_tickets] <- "Network"
comcast_data$ComplaintType[billing_tickets] <- "Billing"
comcast_data$ComplaintType[email_tickets] <- "Email"
comcast_data$ComplaintType[charges_ticket] <- "Charges"
comcast_data$ComplaintType[-c(internet_tickets, network_tickets, billing_tickets, c
harges_ticket, email_tickets)] <- "Others"
我可以在 Python 中轉換如下第一組操作:
network_tickets = df.ComplaintDescription.str.contains ('network', regex=True, case=False)
但是,找到將變數 network_tickets 作為值“Internet”分配到新的 Pandas 資料框列(即 ComplaintType)中的挑戰。在 R 中,似乎你可以在一行中完成。
但是,不確定我們如何在一行代碼中在 Python 中執行此操作,嘗試了以下方法但出現錯誤:
a) df['ComplaintType'].apply(internet_tickets) = "Internet"
b) df['ComplaintType'] = df.apply(internet_tickets)
c) df['ComplaintType'] = internet_tickets.apply("Internet")
我想我們可以先在 dataframe 中創建一個新列:
df['ComplaintType'] = internet_tickets
但不確定下一步。
uj5u.com熱心網友回復:
使用Series.str.contains與DataFrame.loc通過串列設定值:
df = pd.DataFrame(data = {"ComplaintDescription":["BiLLing is super","email","new"]})
L = [ "Internet","Network", "Billing", "Email", "Charges"]
for val in L:
df.loc[df['ComplaintDescription'].str.contains(val, case=False), 'ComplaintType'] = val
df['ComplaintType'] = df['ComplaintType'].fillna('Others')
print (df)
ComplaintDescription ComplaintType
0 BiLLing is super Billing
1 email Email
2 new Others
編輯:
如果需要單獨使用值:
df.loc[df['ComplaintDescription'].str.contains('network', case=False), 'ComplaintType'] = "Internet"
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/338790.html
上一篇:Na填充特定值后
