Date Transaction Id ClientId Services Class
01-10-2021 1234 1 ['Lip Threading' , 'Eye brow threading'] Threading
02-10-2021 1235 2 ['Full face Threading', 'Eye Brow threading'] Threading
03-10-2021 2346 3 ['Eyebrow Threading' , 'Facial' , 'waxing'] Thread and oth
04-10-2021 5432 4 ['Hair cut' , 'Facial'] Other
05-10-2021 6578 5 ['Eye brow threading' , 'Haircut', 'facial'] Thread and oth
06-10-2021 3425 6 ['Head Massage', ' hair cut'] Other
我有包含上述資料的資料框,并且有一列名為 services 的列具有不同的服務作為串列。根據我想對類列進行分類的串列,我的主要目標是對只有執行緒的事務、與其他服務的執行緒、沒有執行緒的其他服務進行分類。
uj5u.com熱心網友回復:
使用 apply 從服務派生類列
def classify(lst):
'''
Classify Type of list
'''
threading = "Threading" if any("threading" in el.lower() for el in lst) else ""
other = "Other" if any("threading" not in el.lower() for el in lst) else ""
if threading and other:
return "Threading and Other"
if threading:
return "Threading"
return "Other"
# Derive Class column from Services column
df['Class'] = df.Services.apply(classify)
輸出
Transaction Id ClientId Services Class
0 01-10-2021 1234 1 ['Lip Threading' , 'Eye brow threading'] Threading
1 02-10-2021 1235 2 ['Full face Threading', 'Eye Brow threading'] Threading
2 03-10-2021 2346 3 ['Eyebrow Threading' , 'Facial' , 'waxing'] Threading and Other
3 04-10-2021 5432 4 ['Hair cut' , 'Facial'] Other
4 05-10-2021 6578 5 [Eye brow threading' , 'Haircut', 'facial'] Threading and Other
5 06-10-2021 3425 6 ['Head Massage', ' hair cut'] Other
完整代碼
from io import StringIO
import pandas as pd
def classify(lst):
threading = "Threading" if any("threading" in el.lower() for el in lst) else ""
other = "Other" if any("threading" not in el.lower() for el in lst) else ""
if threading and other:
return "Threading and Other"
if threading:
return "Threading"
return "Other"
# Derive Dataframe
s = '''Transaction,Id,ClientId,Services
01-10-2021 ,1234,1,"['Lip Threading' , 'Eye brow threading']"
02-10-2021,1235,2,"['Full face Threading', 'Eye Brow threading']"
03-10-2021,2346,3,"['Eyebrow Threading' , 'Facial' , 'waxing']"
04-10-2021,5432,4,"['Hair cut' , 'Facial']"
05-10-2021,6578,5,"'Eye brow threading' , 'Haircut', 'facial']"
06-10-2021,3425,6,"['Head Massage', ' hair cut']"'''
df = pd.read_csv(StringIO(s), sep = ",", quotechar='"')
# Convert Services column to lists
df['Services'] = df.Services.apply(lambda x: x[1:-1].split(','))
# Derive Class column
df['Class'] = df.Services.apply(classify)
uj5u.com熱心網友回復:
它可以用np.select和來完成apply。
示例資料框:
colA colB
0 1 [A, B, C]
1 2 [A]
2 3 [B, C]
3 4 [A, C]
4 5 [B]
狀況
conditions = [
df["colB"].apply(lambda x: ("A" in x) and len(x)==1 ),
df["colB"].apply(lambda x: ("A" in x) and len(x)!=1)
]
創建列
df["Result"] = np.select(conditions,["A","A and others"], default="Others")
最終資料框:
colA colB colC
0 1 [A, B, C] A and others
1 2 [A] A
2 3 [B, C] Others
3 4 [A, C] A and others
4 5 [B] Others
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/311134.html
