python Pandas : How to groupby and count and select a portion of counts?

标签 python pandas count group-by

我有一个这样的 df:

          new_org               old_org    asn  cc
0    85736 pcizzi   85736 - Pcizzi S .a  23201  PY
1             001              001 Host  40244  US
2      85736 blah       85736 - whatevs  23201  PY
3             001        001 IT Complex  55734  IN
4  001 hospedagem   001 Hospedagem Ltda  36351  US
5          001web  action.us.001web.net  36351  US

我想根据“asn”列对我的 df 进行分组,然后选择那些有多于一行的组。我现在就是这样做的,但我不确定它是否正确:

df.groupby('asn').apply(lambda x:x.count()>1)

有人可以帮忙吗?

最佳答案

您可以 filter a group .

试试 df.groupby('asn').filter(lambda x: len(x) > 1),它会返回一个 DataFrame。如有必要,您可以将其进一步分组。

关于 python Pandas : How to groupby and count and select a portion of counts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30255186/

相关文章:

if-statement - 将一系列单元格与单个单元格进行比较,然后递增一个值

python - numpy genfromtxt 或 numpy loadtxt "ValueError: could not convert string to float"或 "too many values to unpack",几乎尝试了所有方法

python - "Execution failed on sql"用于使用 pandas.read_sql 和 MSAccess 的特定表名

python - Sklearn,网格搜索 : how to print out progress during the execution?

python - 如何循环浏览 pandas 数据框列表

sql - 将两个 count(*) 结果相加

python - 在 bash shell 中使用 Python 2.6 从目录中读取文件的正确方法

python - Pandas 。将值与其他 DataFrame 中的相应范围进行匹配

python - 将 Timeseries 的索引从 datetime64[ns] 转换为 datetime64[s] 而不会丢失信息

mysql - 统计每个部门的所有连接用户