group-by - How to group by columns?
问题描述
I am having trouble figuring out how to group rows by columns. My goal is to count the number of 'Package Codes' where column values are orange and blue.
I am working with thousands of rows of data. This is a subset of the data:
Country Package Code Color Type
US 100 Orange a
US 100 Orange b
US 100 Orange c
Mexico 200 Green d
US 300 Blue e
Canada 400 Red f
Germany 500 Red g
Germany 600 Blue h
Desired Output:
Country Packages
US 2
Mexico 0
Canada 0
Germany 1
解决方案
Using isin
+ nunique
+ reindex
(df.loc[df.Color.isin(['Orange', 'Blue'])].groupby('Country')['Package Code']
.nunique().reindex(df.Country.unique(), fill_value=0)).to_frame('Total').reset_index()
Country Total
0 US 2
1 Mexico 0
2 Canada 0
3 Germany 1
Here is the above command broken down a bit for better readability:
# Select rows where the color is Orange or Blue
u = df.loc[df.Color.isin(['Orange', 'Blue'])]
# Find the unique values for Package Code, grouped by Country
w = u.groupby('Country')['Package Code'].nunique()
# Add in missing countries with a value of 0
w.reindex(df.Country.unique(), fill_value=0).to_frame('Total').reset_index()
推荐阅读
- r - 将数据框更改为具有 1 和 0 的另一个数据框
- android - 获得相机权限后不显示预览
- python - 如何将 boxstyle 用于 pad 和 round 用于矩形?
- android - 如何在打开另一个 mapbox 实例之前卸载 mapbox
- java - 为什么 GET 资源没有响应请求
- r - 带有 ggplots 和 treemapify 的图表 Treemap
- angular - Angular 7 无法加载图像
- drupal-8 - 动态实体参考 - info.yml 中缺少核心键
- javascript - 将包含包含的 div 刷新到其他 php 文件
- php - 无法将图像从 URI 上传到 PHP 中的 Firebase 存储