Home > Software engineering >  In a pandas data frame, Is there a way to find the amount of duplicate pairings that exist between r
In a pandas data frame, Is there a way to find the amount of duplicate pairings that exist between r

Time:02-02

For example, you know the possible values that can be under A and those that can be under B and you want to know the number of instance each pairing occurs.

Name A B
First 1 C
Second 2 F
Third 2 C
Fourth 1 C
Fifth 2 F

Output:

1,C = 2

2,F = 2

2,C = 1

CodePudding user response:

As mentioned in the comments, you can groupby count:

grouping = df.groupby(['A', 'B'])['Name'].count()

Output:

>>> grouping
A  B
1  C    2
2  C    1
   F    2
Name: Name, dtype: int64

>>> grouping[(1, 'C')]
2

>>> grouping[(2, 'C')]
1

>>> grouping[(2, 'F')]
2
  •  Tags:  
  • Related