python - 熊猫过滤器分组
问题描述
我想过滤此查询中的结果,因此如果可能在一行代码中,表中只有结果 >1。
import pandas as pd
import numpy as np
df= pd.DataFrame({'Product':['A','B', 'C','A','B','D'],
'Age':[28,39,21,50,35,43],
'Country':['USA','India','Germany','USA','India','India']
})
print(df.head())
table=df.groupby(['Product','Country'])['Age'].count()
table
解决方案
让我们链式query
方法:
table = (df.groupby(['Product','Country'])['Age'].count()
.reset_index(name='Count')
.query('Count > 1'))
table
输出:
Product Country Count
0 A USA 2
1 B India 2
推荐阅读
- sql - 当我运行 vba 宏时没有任何反应?
- python - 如何使用原始文件名保存裁剪的图像?
- c# - 两个返回字符串方法调用另一个方法
- java - 如何在其他 Scala 类中使用处理函数?
- java - Logstash-Logback 编码器:“IllegalAccessError:无法访问类 […]”
- python-3.x - 如何通过 mqtt 发送包含不同数据类型的消息?
- nvidia - cudnn 错误 :: CUDNN_STATUS_SUCCESS (1 vs. 0) CUDNN_STATUS_NOT_INITIALIZED
- html - 防止 div 内的图标折叠到其他 div 内
- blazor - 如何将只读集合传递给 Blazor 组件?
- sqlite - 过滤必须至少存在 1 次的 2 个值的出现