计算最频繁的值并操纵i

2024-04-18 01:26:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有如下数据帧:

            User           Bought 

0           U296             PC
1           U300            Table
2           U296             PC
3           U296            Chair

我想创建两列,一列显示用户购买最多的商品,第二列显示该商品被购买的次数,因此我在最后得到:

            User           Bought         Most_Bought          Times_bought

0           U296             PC               PC                    2
1           U300            Table            Table                  1
2           U296             PC               PC                    2
3           U296            Chair             PC                    2

我知道我应该像groupby一样使用mode(),但我错过了最后一步。你知道吗

谢谢你的帮助!你知道吗


Tags: 数据用户mosttable次数商品groupbypc
2条回答

我花了很长时间才实现:)通过使用value_counts

df[['Most_Bought','Times_bought']]=df.groupby('User').Bought.transform(lambda x : [pd.Series(x).value_counts()\
                             .reset_index().loc[0].values]).apply(pd.Series)
df
Out[231]: 
   User Bought Most_Bought  Times_bought
0  U296     PC          PC             2
1  U300  Table       Table             1
2  U296     PC          PC             2
3  U296  Chair          PC             2

更新:

In [330]: g = df.groupby('User')['Bought']

In [331]: vc = g.value_counts().to_frame(name='Times_bought').reset_index()

In [332]: df = df.merge(vc)

In [333]: df
Out[333]:
   User Bought  Times_bought Most_Bought
0  U296     PC             2          PC
1  U296     PC             2          PC
2  U300  Table             1       Table
3  U296  Chair             1          PC

In [334]: df['Most_Bought'] = df['User'].map(g.agg(lambda x: x.mode()[0]))

In [335]: df
Out[335]:
   User Bought  Times_bought Most_Bought
0  U296     PC             2          PC
1  U296     PC             2          PC
2  U300  Table             1       Table
3  U296  Chair             1          PC

旧答案:

IIUC

In [222]: x = df.groupby('User')['Bought'] \
     ...:       .agg([lambda x: x.mode()[0], 'nunique']) \
     ...:       .rename(columns={'<lambda>':'Most_Bought','nunique':'Times_bought'})
     ...:

In [223]: df.merge(x, left_on='User', right_index=True)
Out[223]:
   User Bought Most_Bought  Times_bought
0  U296     PC          PC             2
2  U296     PC          PC             2
3  U296  Chair          PC             2
1  U300  Table       Table             1

保持原有秩序:

In [258]: df.merge(x, left_on='User', right_index=True).reindex(df.index)
Out[258]:
   User Bought Most_Bought  Times_bought
0  U296     PC          PC             2
1  U300  Table       Table             1
2  U296     PC          PC             2
3  U296  Chair          PC             2

辅助数据框:

In [224]: x
Out[224]:
     Most_Bought  Times_bought
User
U296          PC             2
U300       Table             1

相关问题 更多 >

    热门问题