你可以使用下面的基本语法来计算pandas DataFrame中各组的唯一值的数量。
df.groupby('group_column')['count_column'].nunique()
下面的例子展示了如何在下面的DataFrame中使用这个语法。
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
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
例1:按一列分组并计算唯一值
下面的代码显示了如何计算每支球队的 "积分 "列中的唯一值的数量。
#count number of unique values in 'points' column grouped by 'team' column
df.groupby('team')['points'].nunique()
Name: points, dtype: int64
从输出中我们可以看到。
A队有4个独特的 "积分 "值。
B队有3个独特的 "积分 "值。
注意,我们也可以使用**unique()**函数来显示各队的独特 "积分 "值。
#display unique values in 'points' column grouped by 'team'
df.groupby('team')['points'].unique()
A [5, 7, 9, 12]
B [9, 4, 7]
Name: points, dtype: object
例2:按多列分组和计算唯一值
下面的代码显示了如何计算 "积分 "列中按球队_和_位置分组的唯一值的数量。
#count number of unique values in 'points' column grouped by 'team' and 'position'
df.groupby(['team', 'position'])['points'].nunique()
team position
A F 2
B F 2
Name: points, dtype: int64
从输出中我们可以看到。
A队F位置的球员有2个唯一的 "积分 "值。
A队中G位置的球员有2个独特的 "积分 "值。
B队F位置的球员有2个独特的 "积分 "值。
B队中G位置的球员有1个独特的 "积分 "值。
再一次,我们可以使用**unique()**函数来显示每个球队和位置的独特'积分'值。
#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
下面的教程解释了如何在pandas中执行其他常见操作。
Pandas:如何查找一列中的唯一值
Pandas:如何查找多列中的唯一值
Pandas:如何计算特定值在列中的出现次数
The postHow to Count Unique Values Using Pandas GroupByappeared first onStatology.