Pandas+groupby用法讲解

简介:
import pandas as pd
import numpy as np
  • 1
  • 2
#导入数据
type_specified={"trip_id":"object","bikeid":"object","from_station_id":"object","to_station_id":"object",
               "hour":"object","weekday":"object"}
ridership=pd.read_csv(r"E:\chicago_bikes_data\txt&csv\ridership_2016.txt",
                      nrows=10000,dtype=type_specified,usecols=range(1,15))
  • 1
  • 2
  • 3
  • 4
  • 5
ridership.head()
  • 1
trip_id starttime stoptime bikeid tripduration from_station_id from_station_name to_station_id to_station_name usertype gender birthyear hour weekday
0 9080551 3/31/2016 23:53 4/1/2016 0:07 155 841 344 Ravenswood Ave & Lawrence Ave 458 Broadway & Thorndale Ave Subscriber Male 1986.0 23 4
1 9080550 3/31/2016 23:46 3/31/2016 23:57 4831 649 128 Damen Ave & Chicago Ave 213 Leavitt St & North Ave Subscriber Male 1980.0 23 4
2 9080549 3/31/2016 23:42 3/31/2016 23:46 4232 210 350 Ashland Ave & Chicago Ave 210 Ashland Ave & Division St Subscriber Male 1979.0 23 4
3 9080548 3/31/2016 23:37 3/31/2016 23:55 3464 1045 303 Broadway & Cornelia Ave 458 Broadway & Thorndale Ave Subscriber Male 1980.0 23 4
4 9080547 3/31/2016 23:33 3/31/2016 23:37 1750 202 334 Lake Shore Dr & Belmont Ave 329 Lake Shore Dr & Diversey Pkwy Subscriber Male 1969.0 23 4
#groupby可以通过传入需要分组的参数实现对数据的分组
ridership_user=ridership.groupby("usertype")
ridership_user_week=ridership.groupby(["usertype","weekday"])
  • 1
  • 2
  • 3
#groupby之后的数据并不是DataFrame格式的数据,而是特殊的groupby类型,此时,可以通过size()方法返回分组后的记录数目统计结果,该结果
#是Series类型
ridership_user.size()
  • 1
  • 2
  • 3
usertype
Customer      1023
Subscriber    8977
dtype: int64

  • 1
  • 2
  • 3
  • 4
  • 5
ridership_user_week.size()
  • 1
usertype    weekday
Customer    3           286
            4           737
Subscriber  3          2714
            4          6263
dtype: int64

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
#可以通过索引的方式实现对分组后各类别记录数的提取
ridership_user_week.size().loc["Customer","3"]
  • 1
  • 2
286
#通过groupby完成对数据的分组后,可以通过get_group方法来获取某一制定分组的结果
  • 1
ridership_user.get_group("Customer").head()
  • 1
trip_id starttime stoptime bikeid tripduration from_station_id from_station_name to_station_id to_station_name usertype gender birthyear hour weekday
23 9080526 3/31/2016 23:04 3/31/2016 23:32 1638 1648 505 Winchester Ave & Elston Ave 505 Winchester Ave & Elston Ave Customer NaN NaN 23 4
65 9080481 3/31/2016 22:24 3/31/2016 22:45 3438 1220 152 Lincoln Ave & Diversey Pkwy 316 Damen Ave & Sunnyside Ave Customer NaN NaN 22 4
77 9080463 3/31/2016 22:15 3/31/2016 22:17 3464 97 47 State St & Kinzie St 47 State St & Kinzie St Customer NaN NaN 22 4
113 9080426 3/31/2016 22:02 3/31/2016 22:29 2716 1582 340 Clark St & Wrightwood Ave 161 Rush St & Superior St Customer NaN NaN 22 4
115 9080424 3/31/2016 22:02 3/31/2016 22:29 1052 1611 340 Clark St & Wrightwood Ave 161 Rush St & Superior St Customer NaN NaN 22 4
#对于多层分组后的数据,如果需要使用多重索引,此处需要传入一个元组
ridership_user_week.get_group(("Customer","3")).head()
  • 1
  • 2
trip_id starttime stoptime bikeid tripduration from_station_id from_station_name to_station_id to_station_name usertype gender birthyear hour weekday
7002 9071238 3/30/2016 23:57 3/31/2016 0:03 4018 317 305 Western Ave & Division St 374 Western Ave & Walton St Customer NaN NaN 23 3
7007 9071233 3/30/2016 23:51 3/30/2016 23:56 4018 316 374 Western Ave & Walton St 305 Western Ave & Division St Customer NaN NaN 23 3
7014 9071226 3/30/2016 23:28 3/30/2016 23:45 1142 993 71 Morgan St & Lake St 15 Racine Ave & 18th St Customer NaN NaN 23 3
7117 9071031 3/30/2016 20:13 3/30/2016 20:34 1919 1302 284 Michigan Ave & Jackson Blvd 173 Mies van der Rohe Way & Chicago Ave Customer NaN NaN 20 3
7118 9071030 3/30/2016 20:13 3/30/2016 20:34 2909 1312 284 Michigan Ave & Jackson Blvd 173 Mies van der Rohe Way & Chicago Ave Customer NaN NaN 20 3
#对于分组后的数据,如果还需要进行一系列查询,可以使用apply方法,传入一个处理的函数
#假设我们需要查找上述分组后每组中用时最长的一组记录的起始和终止站点,可执行如下命令
  • 1
  • 2
max_time_from_id=ridership_user_week.apply(lambda record:record["from_station_id"][record["tripduration"].idxmax()])
max_time_to_id=ridership_user_week.apply(lambda record:record["to_station_id"][record["tripduration"].idxmax()])
max_time_time=ridership_user_week.apply(lambda record:record["tripduration"][record["tripduration"].idxmax()])
max_time_ridership=pd.DataFrame({"from_station_id":max_time_from_id,"to_station_id":max_time_to_id,
                                "tripduratio":max_time_time})
  • 1
  • 2
  • 3
  • 4
  • 5
max_time_ridership
  • 1
from_station_id to_station_id tripduratio
usertype weekday
Customer 3 35 277 81702
4 340 229 68571
Subscriber 3 287 254 71758
4 133 49 70977

目录
相关文章
|
5月前
|
数据处理 索引 Python
Pandas中concat的用法
Pandas中concat的用法
138 1
|
5月前
|
存储 数据采集 数据处理
深入探索Pandas的DataFrame:基本用法与案例研究
深入探索Pandas的DataFrame:基本用法与案例研究
118 0
|
7天前
|
数据挖掘 Python
pandas中的groupby函数应用
pandas中的groupby函数应用
pandas中的groupby函数应用
|
7天前
|
数据挖掘 数据处理 Python
Pandas中groupby后的数据排序技巧
Pandas中groupby后的数据排序技巧
12 0
|
7天前
|
数据采集 运维 数据挖掘
Pandas中的Rank用法:数据排序的高效工具
Pandas中的Rank用法:数据排序的高效工具
15 0
|
7天前
|
索引 Python
Pandas中的时间序列利器:set_index用法
Pandas中的时间序列利器:set_index用法
20 0
|
2月前
|
SQL 数据挖掘 数据处理
不再纠结,一文详解pandas中的map、apply、applymap、groupby、agg...
不再纠结,一文详解pandas中的map、apply、applymap、groupby、agg...
|
3月前
|
数据采集 数据挖掘 数据格式
Pandas的用法
【7月更文挑战第9天】Pandas的用法
25 3
|
2月前
|
索引 Python
【Pandas】Pandas Dataframe 常用用法
Pandas DataFrame的常用操作示例,包括筛选数据、索引操作、合并DataFrame、设置和排序索引、文本处理、列重命名、处理缺失值、排序以及删除满足特定条件的行等技巧。
41 0
|
4月前
|
Python
pandas中groupby和shift结合实现相邻行的计算
pandas中groupby和shift结合实现相邻行的计算
50 0
下一篇
无影云桌面