Pandas之分组
假如我们现在有这样一组数据:星巴克在全球的咖啡店信息,如下图所示。数据来源:starbucks_store_locations。我们想要统计中国每个城市的星巴克商店的数量,那我们应该怎么做呢?

在pandas中,为我们提供了一个处理分组问题的函数groupby():
# coding=utf-8
import pandas as pd
file_path = "./starbucks_store_worldwide.csv"
# 设置最多输出20行
pd.set_option('display.max_rows',20)
df = pd.read_csv(file_path)
df_CN = df[df["Country"]=="CN"]
grouped_CN = df_CN.groupby(by="City")
print(grouped_CN["Brand"].count())
结果如下:
City
Admiralty 2
Causeway Bay 5
Central 1
Chaiwan 1
Changshu 1
Changzhou 1
Fortress Hill 1
Hangzhou 2
Hong Kong 104
Jiaxing 2
...
长春市 10
长沙市 26
阳江市 1
青岛市 28
靖江市 2
鞍山市 3
马鞍山 3
高邮市 1
黄石市 1
龙岩市 2
Name: Brand, Length: 197, dtype: int64
Process finished with exit code 0
假如我们不想先提取中国的数据再分组,而是想一步到位应该怎么做呢?
# coding=utf-8
import pandas as pd
file_path = "./starbucks_store_worldwide.csv"
# 设置最多输出20行
pd.set_option('display.max_rows',20)
df = pd.read_csv(file_path)
grouped = df[df["Country"] == "CN"]["Brand"].groupby(by=[df["Country"], df["City"]]).count()
print(grouped)
结果如下:
Country City
CN Admiralty 2
Causeway Bay 5
Central 1
Chaiwan 1
Changshu 1
Changzhou 1
Fortress Hill 1
Hangzhou 2
Hong Kong 104
Jiaxing 2
...
长春市 10
长沙市 26
阳江市 1
青岛市 28
靖江市 2
鞍山市 3
马鞍山 3
高邮市 1
黄石市 1
龙岩市 2
Name: Brand, Length: 197, dtype: int64
我们可以发现,与上面结果不同的是,这次的索引成了2列,不但有city,还有Country。这是因为我们再用group分组的时候传入了2个参数进去。
这里需要注意,最终的结果其实是包含双列索引的单列1维数组,类型是pandas.core.series.Series
Pandas之分组的更多相关文章
- Pandas | GroupBy 分组
任何分组(groupby)操作都涉及原始对象的以下操作之一: 分割对象 应用一个函数 结合的结果 在许多情况下,我们将数据分成多个集合,并在每个子集上应用一些函数.在应用函数中,可以执行以下操作: 聚 ...
- pandas之分组计算笔记
分组计算三部曲:拆分-->应用-->合并 分组:就是按照行或列把相同索引的部分分到一起 分组的关键词为groupby,分组后我们就可以对每组数据进行同一操作,返回的是每组数据分别计算后的结 ...
- pandas groupby 分组操作
最一般化的groupby 方法是apply. tips=pd.read_csv('tips.csv') tips[:5] 新生成一列 tips['tip_pct']=tips['tip']/tips[ ...
- pandas用法之二
1,函数应用 ①map() 将函数作用于一个Series的每一个函数(不能是DataFrame) 类似于Python的高阶函数map() 函数可以是Numpy中的通用函数,也可以是自定义函数 优点:代 ...
- Pandas模块:表计算与数据分析
目录 Pandas之Series Pandas之DataFrame 一.pandas简单介绍 1.pandas是一个强大的Python数据分析的工具包.2.pandas是基于NumPy构建的. 3.p ...
- 机器学习三剑客之Pandas
pandas Pandas是基于Numpy开发出的,专门用于数据分析的开源Python库 Pandas的两大核心数据结构 Series(一维数据) Series 创建Series的方法 ...
- Pandas:表计算与数据分析
目录 Pandas之Series Pandas之DataFrame 一.pandas简单介绍 1.pandas是一个强大的Python数据分析的工具包.2.pandas是基于NumPy构建的. 3.p ...
- (数据科学学习手札69)详解pandas中的map、apply、applymap、groupby、agg
*从本篇开始所有文章的数据和代码都已上传至我的github仓库:https://github.com/CNFeffery/DataScienceStudyNotes 一.简介 pandas提供了很多方 ...
- 利用Python进行数据分析:【Pandas】(Series+DataFrame)
一.pandas简单介绍 1.pandas是一个强大的Python数据分析的工具包.2.pandas是基于NumPy构建的.3.pandas的主要功能 --具备对其功能的数据结构DataFrame.S ...
随机推荐
- 18 南京 D
裸的最小球覆盖. 坐标范围大一些所以我们把初始的温度也设置的大一些. #include <bits/stdc++.h> using namespace std; typedef long ...
- RabbitMQ之路由键转发消息
RabbitMQ学习 参考:https://www.jianshu.com/p/6b62a0ed2491 消息队列:目前流行的有 Kafka.RabbitMQ.ActiveMQ等 功能:为了解决消息的 ...
- windows安装tomcat
1.打开官网http://tomcat.apache.org/ 2.在左侧的导航栏Download下方选择最新的Tomcat 9,点击页面下方的“ 64-bit Windows zip (pgp, m ...
- org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password:
tationProcessor' to allow for resolving potential circular referencesDEBUG 2018-05-28 11:32:35,016 o ...
- 9 个用于移动APP开发的顶级 JavaScript 框架【申明:来源于网络】
9 个用于移动APP开发的顶级 JavaScript 框架[申明:来源于网络] 地址:http://www.codeceo.com/article/9-app-javascript-framework ...
- Code Generation => Table -> Class for DataGridView use
Generate a class from table defintion and simplify databinding process.
- java 中重写toString()方法
toString()方法 一般出现在System.out.println(类名.toString()); toString()是一种自我描述方法 本身返回的是 getClass().getName() ...
- c语言实现wc功能
本随笔对网站http://blog.chinaunix.net/uid-22566367-id-381958.html有所借鉴 #include <stdio.h> #define BEG ...
- jmeter解决响应乱码
打开apache-jmeter-2.11\bin\jmeter.properties文件,搜索“encoding”关键字,找到如下配置: # The encoding to be used if no ...
- typescript interface 泛型
interface interface Obj { [index: string]: any; } class Person { name: string; } let obj: obj = { na ...