簡體   English   中英

Python pandas 根據一列的唯一值創建多列

[英]Python pandas create multiple columns based on unique values of one column

我有一個熊貓數據框,

account_id, campaign_objective, campaign_spend, conversions
__________, __________________, ______________, ___________
1,          sales,              100,            25
1,          brand,              50,             25
2,          sales,              80,             12
2,          brand,              60,             12

我想做的是為每個獨特的campaign_objective 創建一列,並為其分配相應的支出值。

account_id, sales, brand, conversions
__________, _____, _____, ___________
1,          100,   50,    25
2,          80,    60,    12 

我的方法一直是使用 for 循環和字典。 這是次優的,因為我的數據框中有 2000 萬行,並且有 100 個活動目標; 換句話說,我的 for 循環將需要遍歷 20 億個值。

new_df = {'account_id':[], 'conversions':[]}
for obj in obj_goal_list:
    new_df.update({obj:[]})

for acct in df['account_id'].unique():
    acct_df = df[df['account_id']==acct]
    new_df['account_id'].append(acct)
    new_df['conversions'].append(acct_df['conversions'])

    for obj in obj_goal_list: 
        if obj in acct_df['objective_and_goal']:
            spend = acct_df[acct_df['objective_and_goal']==obj]['spend']
            new_df[obj].append(spend)
        else:
            new_df[obj].append(0.0)
new_df = pd.DataFrame(new_df)

我很好奇是否有更多的“熊貓”方式通過樞軸或其他方式來實現這一目標?

樞軸方法可能會對您有所幫助。

df.pivot(index = ['account_id', 'conversions'], columns = 'campaign_objective', values = 'campaign_spend').reset_index()

這是一種方法

df.pivot(index=['account_id','conversions'], columns='campaign_objective', values='campaign_spend')

     campaign_objective     brand   sales
account_id  conversions         
         1           25        50     100
         2           12        60      80

使用 reset_index

df.pivot(index=['account_id','conversions'], columns='campaign_objective', values='campaign_spend').reset_index()
campaign_objective  account_id  conversions     brand   sales
                 0           1           25        50     100
                 1           2           12        60      80

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM