current position:Home>How to calculate unique values using pandas groupby

How to calculate unique values using pandas groupby

2022-01-30 11:52:10 Stone sword


You can use the following basic syntax to calculate pandas DataFrame The number of unique values in each group .

df.groupby('group_column')['count_column'].nunique()

 Copy code 

The following example shows how to DataFrame Use this syntax in .

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'G', 'G', 'F', 'F', 'G', 'G', 'F', 'F', 'F'],
                   'points': [5, 7, 7, 9, 12, 9, 9, 4, 7, 7],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12, 13, 15]})

#view DataFrame
df

	team	position points	rebounds
0	A	G	 5	11
1	A	G	 7	8
2	A	G	 7	10
3	A	F	 9	6
4	A	F	 12	6
5	B	G	 9	5
6	B	G	 9	9
7	B	F	 4	12
8	B	F	 7	13
9	B	F	 7	15

 Copy code 

example 1: Group by a column and calculate unique values

The following code shows how to calculate the of each team " integral " The number of unique values in the column .

#count number of unique values in 'points' column grouped by 'team' column
df.groupby('team')['points'].nunique()

team
A    4
B    3
Name: points, dtype: int64

 Copy code 

We can see from the output .

  • A Team has 4 individual Distinctive " integral " value .
  • B Team has 3 individual Distinctive " integral " value .

Be careful , We can also use **unique()** Show the unique functions of each team " integral " value .

#display unique values in 'points' column grouped by 'team'
df.groupby('team')['points'].unique()

team
A    [5, 7, 9, 12]
B        [9, 4, 7]
Name: points, dtype: object
 Copy code 

example 2: Group and calculate unique values by multiple columns

The following code shows how to calculate " integral " Column by team _ and _ Number of unique values for location grouping .

#count number of unique values in 'points' column grouped by 'team' and 'position'
df.groupby(['team', 'position'])['points'].nunique()

team  position
A     F           2
      G           2
B     F           2
      G           1
Name: points, dtype: int64
 Copy code 

We can see from the output .

  • A team F The players in the position are 2 individual Unique " integral " value .
  • A In the team G The players in the position are 2 individual Distinctive " integral " value .
  • B team F The players in the position are 2 individual Distinctive " integral " value .
  • B In the team G The players in the position are 1 individual Distinctive " integral " value .

Once again, , We can use **unique()** Function to display the unique characteristics of each team and position ' integral ' value .

#display unique values in 'points' column grouped by 'team' and 'position'
df.groupby(['team', 'position'])['points'].unique()

team  position
A     F           [9, 12]
      G            [5, 7]
B     F            [4, 7]
      G               [9]
Name: points, dtype: object
 Copy code 

Other resources

The following tutorial explains how to pandas Perform other common operations in .

Pandas: How to find a unique value in a column
Pandas: How to find unique values in multiple columns
Pandas: How to calculate a specific value in a column Number of occurrences of

The postHow to Count Unique Values Using Pandas GroupByappeared first onStatology.

copyright notice
author[Stone sword],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301152078509.html

Random recommended