grouping and sorting

grouping and sorting

group

本文中的数据案例来自Kaggle,数据格式如下
reviews

是关于酒的表

groupby的用法

reviews.groupby('points').points.count()  #返回series,值为每个分数有多少个
>>>
points
80     397
81     692
      ... 
99      33
100     19
Name: points, Length: 21, dtype: int64

代码:
reviews.groupby('points').price.min()  #返回series,值为每个分数中,价格最低的
>>>
points
80      5.0
81      5.0
       ... 
99     44.0
100    80.0
Name: price, Length: 21, dtype: float64

reviews.groupby('winery').apply(lambda df: df.title.iloc[0])  # apply的操作对象是df
>>>
winery
1+1=3                          1+1=3 NV Rosé Sparkling (Cava)
10 Knots                 10 Knots 2010 Viognier (Paso Robles)
                                  ...                        
àMaurice    àMaurice 2013 Fred Estate Syrah (Walla Walla V...
Štoka                         Štoka 2009 Izbrani Teran (Kras)
Length: 16757, dtype: object

# 筛选每个国家,每个地区评价最高的酒
reviews.groupby(['country', 'province']).apply(lambda df: df.loc[df.points.idxmax()]) 

结果如下图
结果
groupby与agg函数合用

reviews.groupby(['country']).price.agg([len, min, max])
>>>
	               len	min	max
country			
Argentina	3800	4.0	    230.0
Armenia	       2	14.0	15.0
...	...	...	...
Ukraine	      14	6.0  	13.0
Uruguay	     109	10.0	130.0

##多层索引
countries_reviewed = reviews.groupby(['country', 'province']).description.agg([len])

>>>
		                        len
country	    		province	
Argentina	Mendoza Province	3264
                       Other	536
...	                     ...	...
Uruguay	            San Jose    3
					Uruguay	    24
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值