代码之家  ›  专栏  ›  技术社区  ›  jbuddy_13

Python根据一列的唯一值创建多个列

  •  0
  • jbuddy_13  · 技术社区  · 2 年前

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

    我想做的是为每个独特的campaign_目标创建一列,并为其分配相应的支出价值。

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

    我的方法一直用于循环和字典。这是次优的,因为我的数据帧中有2000万行,有100个campaign\u目标;换句话说,我的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)
    

    我很好奇,是否有更“熊猫”的方式来实现这一点,通过枢轴或其他方式?

    2 回复  |  直到 2 年前
        1
  •  1
  •   Naveed    2 年前

    这里有一种方法

    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_索引

    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
    
        2
  •  1
  •   user1234567890    2 年前

    透视法可能对您有所帮助。

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