Python Pandas : Group by one column and see the content of all columns?

标签 python pandas group-by aggregation

我有一个像这样的数据框:

org     group   count
org1      1       2
org2      1       2
org3      2       1
org4      3       3
org5      3       3
org6      3       3

这就是我想要的,“组”列中每个唯一组的一个条目:

org     group   count
org1      1       2
org3      2       1
org4      3       3

我正在使用以下分组命令,但我仍然可以看到所有行:

df.groupby('group').head()

有人知道如何获得预期的结果吗?

最佳答案

您可以在drop_duplicates吗?

In [172]: df.drop_duplicates('group')
Out[172]:
    org  group  count
0  org1      1      2
2  org3      2      1
3  org4      3      3

此外,df.drop_duplicates(['group', 'count']) 在这种情况下也有效。

但是,这可能不是最好的一种非常灵活的方法。 @EdChum的Answer提供灵活性方向。

关于Python Pandas : Group by one column and see the content of all columns?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828675/

相关文章:

python - 使用 Python asyncore 对网站进行负载/压力测试

python - Dash DataTable 前端过滤器中的复合运算符

pandas - 如何在 vscode 中打印出 Pandas 数据帧

sql - 在多对多查询中选择 TOP 1

python - 计算python中每个目标变量的百分比

python - 没有任何循环的 Scipy 快速一维插值

Python 子进程 - 写入多个标准输入

python - 检查 header 是否存在于 Python pandas

python - 在 Python 中从 Spark DataFrame 创建 labeledPoints

MySQL 按主题分组