【问题标题】:How to filter and groupby in python如何在python中过滤和分组
【发布时间】:2017-07-24 04:28:57
【问题描述】:

我有一个数据集(作为示例组成如下),我正在尝试同时进行分组和过滤。我想按职业分组,然后只过滤男性的性别。我也在 Pandas 工作。

Occupation             Age    Sex
Accountant              23     Female
Doctor                  33     Male
Accountant              43     Male
Doctor                  28     Female

我希望最终结果看起来像这样:

Occupation    Sex
Accountant     1
Doctor         1

到目前为止,我已经提出了以下建议,但它并没有过滤男性的性行为

data.groupby(['occupation'])[['sex']].count()    

谢谢。

【问题讨论】:

  • 这是熊猫吗??
  • 是的。刚刚编辑了。谢谢。

标签: python pandas group-by filtering


【解决方案1】:

groupby之前使用query

data.query('Sex == "Male"').groupby('Occupation').Sex.size().reset_index()

【讨论】:

  • 编辑:没关系,我在输入时错过了。现在可以使用了,谢谢!
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 2021-09-01
  • 2019-01-13
  • 2020-11-05
相关资源
最近更新 更多