Pandas:如何使用 groupby 选项获取列中每个值的计数

发布于 01-12 19:50 字数 1712 浏览 2 评论 0原文

这是我得到的数据框:

data = {'Year' : [2021, 2021, 2021, 2022, 2022, 2022], 
        'Class':['A', 'A', 'B', 'A', 'C', 'C'], 
        'Animal':['dog|cat|bird', 'cat|dog', 'tiger|dog', 'cat|bird', 'dog|cat|rabbit', 'rabbit|dog|tiger',]}
df = pd.DataFrame(data)

所以 df 看起来像:

YearClassAnimal
2021Adog|cat|bird
2021Acat|dog
2021BTiger|dog
2022Acat|bird
2022Cdogs|cat|
2022Crabbitrabbit |dog|tiger

我想做的是计算每个年份和班级中每种动物的数量。例如,我想获取以下数据框:

YearClassAnimalCount
2021Adogs2
2021Acat2
2021ABird1
2021BTiger1
2021BDog1
2022Acat1
2022ABird1
2022CDog2
2022Ccat1
2022C2
2022C1

有有人对实现这一目标有什么建议吗?我会非常感激。

This is the dataframe I've got:

data = {'Year' : [2021, 2021, 2021, 2022, 2022, 2022], 
        'Class':['A', 'A', 'B', 'A', 'C', 'C'], 
        'Animal':['dog|cat|bird', 'cat|dog', 'tiger|dog', 'cat|bird', 'dog|cat|rabbit', 'rabbit|dog|tiger',]}
df = pd.DataFrame(data)

So the df looks like:

YearClassAnimal
2021Adog|cat|bird
2021Acat|dog
2021Btiger|dog
2022Acat|bird
2022Cdog|cat|rabbit
2022Crabbit|dog|tiger

What I'd like to do is to calculate the number of each animal in each year and class. For example, I want to get the following dataframe:

YearClassAnimalCount
2021Adog2
2021Acat2
2021Abird1
2021Btiger1
2021Bdog1
2022Acat1
2022Abird1
2022Cdog2
2022Ccat1
2022Crabbit2
2022Ctiger1

Does anyone have any suggestions about achieving this? I'd be really appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后2025-01-19 19:50:01

您可以使用一行代码来完成此操作:

(df.assign(Animal=df['Animal'].str.split('|'))     # Create a list using split
  .explode('Animal')                               # Expand that list it rows using explode
  .value_counts(sort=False)                        # Use pd.DataFrame.value_counts 
  .rename('Count')                                 # Rename series
  .reset_index())                                  # Reset to a dataframe

输出:

    Year Class  Animal  Count
0   2021     A    bird      1
1   2021     A     cat      2
2   2021     A     dog      2
3   2021     B     dog      1
4   2021     B   tiger      1
5   2022     A    bird      1
6   2022     A     cat      1
7   2022     C     cat      1
8   2022     C     dog      2
9   2022     C  rabbit      2
10  2022     C   tiger      1

You can do this with a one-liner:

(df.assign(Animal=df['Animal'].str.split('|'))     # Create a list using split
  .explode('Animal')                               # Expand that list it rows using explode
  .value_counts(sort=False)                        # Use pd.DataFrame.value_counts 
  .rename('Count')                                 # Rename series
  .reset_index())                                  # Reset to a dataframe

Output:

    Year Class  Animal  Count
0   2021     A    bird      1
1   2021     A     cat      2
2   2021     A     dog      2
3   2021     B     dog      1
4   2021     B   tiger      1
5   2022     A    bird      1
6   2022     A     cat      1
7   2022     C     cat      1
8   2022     C     dog      2
9   2022     C  rabbit      2
10  2022     C   tiger      1
神爱温柔2025-01-19 19:50:01

让我们尝试 str.get_dummies 然后 groupby

out = (df.Animal.str.get_dummies('|')
       .groupby([df['Year'],df['Class']]).sum()
       .mask(lambda x : x==0)
       .rename_axis(['animal'],axis=1).stack().reset_index(name='Count')
Out[666]: 
    Year Class  animal  Count
0   2021     A    bird    1.0
1   2021     A     cat    2.0
2   2021     A     dog    2.0
3   2021     B     dog    1.0
4   2021     B   tiger    1.0
5   2022     A    bird    1.0
6   2022     A     cat    1.0
7   2022     C     cat    1.0
8   2022     C     dog    2.0
9   2022     C  rabbit    2.0
10  2022     C   tiger    1.0

Let us try str.get_dummies then groupby

out = (df.Animal.str.get_dummies('|')
       .groupby([df['Year'],df['Class']]).sum()
       .mask(lambda x : x==0)
       .rename_axis(['animal'],axis=1).stack().reset_index(name='Count')
Out[666]: 
    Year Class  animal  Count
0   2021     A    bird    1.0
1   2021     A     cat    2.0
2   2021     A     dog    2.0
3   2021     B     dog    1.0
4   2021     B   tiger    1.0
5   2022     A    bird    1.0
6   2022     A     cat    1.0
7   2022     C     cat    1.0
8   2022     C     dog    2.0
9   2022     C  rabbit    2.0
10  2022     C   tiger    1.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文