【问题标题】:Show unique count of specific values in column, groupby an additional column using pandas显示列中特定值的唯一计数,使用熊猫按附加列分组
【发布时间】:2021-03-11 04:02:12
【问题描述】:

我有一个数据集 df,我想在其中按一列分组,显示每个唯一值的计数并显示正确的列。

数据

Id      location
e-db    ny
e-db    ny
e-db    ny
f-a     ny
f-a     ny
gr-x    ca

想要的

Id      location    count
e-db    ny          3
f-a     ny          2
gr-x    ca          1

在做

df.groupby('location')['Id'].nunique()

但是,这并没有向我显示唯一 ID 和实际值。 任何建议表示赞赏

【问题讨论】:

  • df.groupby('Id')['location'].count() ?
  • 嗨,谢谢@ashkangh,但它没有提出位置
  • df.groupby(['Id','location']).size().reset_index(name='count').

标签: python pandas numpy pandas-groupby


【解决方案1】:

您也可以这样做:

df2 = pd.DataFrame()
df2['location'] = df.groupby('Id')['location'].nth(0)
df2['count'] = df.groupby('Id').count()

输出:

Id  location    count
e-db    ny       3
f-a     ny       2
gr-x    ca       1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2021-10-30
    • 2023-01-03
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多