Get row value of maximum count after applying grou

2020-07-27 11:37发布

I have the following df

>In [260]: df
>Out[260]:
    size market vegetable  confirm availability
0  Large    ABC    Tomato                   NaN
1  Large    XYZ    Tomato                   NaN
2  Small    ABC    Tomato                   NaN
3  Large    ABC     Onion                   NaN
4  Small    ABC     Onion                   NaN
5  Small    XYZ     Onion                   NaN
6  Small    XYZ     Onion                   NaN
7  Small    XYZ   Cabbage                   NaN
8  Large    XYZ   Cabbage                   NaN
9  Small    ABC   Cabbage                   NaN

1) How to get the size of a vegetable whose size count is maximum?

I used groupby on vegetable and size to get the following df But I need to get the rows which contain the maximum count of size with vegetable

In [262]: df.groupby(['vegetable','size']).count()
Out[262]:                 market  confirm availability
vegetable size
Cabbage   Large       1                     0
          Small       2                     0
Onion     Large       1                     0
          Small       3                     0
Tomato    Large       2                     0
          Small       1                     0

df2['vegetable','size'] = df.groupby(['vegetable','size']).count().apply( some logic )

Required Df :

  vegetable   size   max_count
0   Cabbage   Small     2
1     Onion   Small     3
2    Tomato   Large     2

2) Now I can say 'Small Cabbages' are available in huge quantity from df. So I need to populate the confirm availability column with small for all cabbage rows How to do this?

    size market vegetable  confirm availability
0  Large    ABC    Tomato                   Large
1  Large    XYZ    Tomato                   Large
2  Small    ABC    Tomato                   Large
3  Large    ABC     Onion                   Small
4  Small    ABC     Onion                   Small
5  Small    XYZ     Onion                   Small
6  Small    XYZ     Onion                   Small
7  Small    XYZ   Cabbage                   Small    
8  Large    XYZ   Cabbage                   Small    
9  Small    ABC   Cabbage                   Small

3条回答
姐就是有狂的资本
2楼-- · 2020-07-27 12:17

You can assign the grouped dataframe to another object, then you can do other grouping on index of 'Vegetable' to get the maximum required value

d = df.groupby(['vegetable','size']).count()
d.groupby(d.index.get_level_values(0).tolist()).apply(lambda x:x[x.confirm == x.confirm.max()])

Out:

                     market confirm availability
vegetable   size            
Cabbage Cabbage Small   2   2   0
Onion   Onion   Small   3   3   0
Tomato  Tomato  Large   2   2   0
查看更多
爷的心禁止访问
3楼-- · 2020-07-27 12:29

You can GroupBy with count, then sort and drop duplicates:

res = df.groupby(['size', 'vegetable'], as_index=False)['market'].count()\
        .sort_values('market', ascending=False)\
        .drop_duplicates('vegetable')

print(res)

    size vegetable  market
4  Small     Onion       3
2  Large    Tomato       2
3  Small   Cabbage       2
查看更多
Anthone
4楼-- · 2020-07-27 12:31

1)

required_df = veg_df.groupby(['vegetable','size'], as_index=False)['market'].count()\
         .sort_values(by=['vegetable', 'market'])\
         .drop_duplicates(subset='vegetable', keep='last')

2)

merged_df = veg_df.merge(required_df, on='vegetable')
cols = ['size_x', 'market_x', 'vegetable', 'size_y']
dict_renaming_cols = {'size_x': 'size', 
                      'market_x': 'market',
                      'size_y': 'confirm_availability'}
merged_df = merged_df.loc[:,cols].rename(columns=dict_renaming_cols)
查看更多
登录 后发表回答