[英]Create multiple columns based on multiple column conditions from another dataframe
[英]Create column based on multiple conditions on different columns
基於來自不同列的值的多個條件,在數據框中創建一列。
目的是指示第一個有趣的動作是何時發生的,這將在t0下用1表示。
數據框的結構如下:
cust_id first_act prod_1 prod_2 t0
0 1 1 1
22 2
23 2 1
24 2 1
25 2
26 3 1
27 3
28 3
29 4
30 4
我想基於以下條件為列t0分配一個值:
如果客戶在prod_1下有1:在值為0時在prod_1下有1的值分配給t0。
如果客戶在prod_1下沒有1,請檢查客戶在prod_2下是否有1,並且如果為true,則在條件為true的索引處為t0分配值1。
最后:如果客戶沒有prod_1或prod_2,但在first_act下確實有1,則在t0下將值1分配給first act為true的索引。
在這些條件之后,每個客戶在t0中應該只有一個值。
cust_id 2的預期輸出:
cust_id first_act prod_1 prod_2 t0
0 1 1 1
22 2 1
23 2 1
24 2 1 1
25 2
26 3 1
27 3
28 3
29 4
30 4
我嘗試使用嵌套的np.where語句來執行此操作,但該操作不適用於以下情況:
df['t0'] = np.where(df['prod_1'] == 1, 1 ,
np.where(df['prod_2'] == 1, 1,
np.where(df['first_act'] == 1, 1, 0)))
在多個位置的t0上加1。
更新資料
@Jeffyx我不知道這是否可以解決,但是我想到的是:
if prod_1 == 1:
t0 = 1 at index of prod_1 == 1
if not prod_1 == 1:
if prod_2 == 1:
t0 = 1 at index of prod_2 == 1
if not prod_1 == 1 and not prod_2 == 1:
if first_act == 1:
t0 = 1 at index of first_act == 1
您必須找到符合條件的第一個索引,然后使用該索引在t0
列中設置一個值。
使用groupby,它可以:
for _, sub in df.groupby(['cust_id']): # test for each cust_id
for col in ['prod_1', 'prod_2', 'first_act']: # test columns in sequence
tmp = sub[sub[col] == 1] # try to match
if len(tmp) != 0: # ok found at least one
df.loc[tmp.index[0], 't0'] = 1 # set t0 to 1 for first index found
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.