以o2o大赛的数据为例
df_new = df[['User_id','Merchant_id']]
df_new_1 = df_new.groupby(['User_id'])['Merchant_id'].nunique()
在同一个'User_id'下,'Merchant_id'有多少个
df_new_1 = df_new.groupby(['User_id'])['Merchant_id'].value_counts()
Name: Merchant_id, dtype: int64
df_new_1 = df_new.groupby(['User_id'])['Merchant_id'].unique()
返回具体的unique值
Series.
nunique
(dropna=True)[source]
Parameters: | dropna : boolean, default True
|
---|---|
Returns: |
|
Return number of unique elements in the object.
Excludes NA values by default
源码
def
nunique
(
self
,
dropna
=
True
):
"""
Return number of unique elements in the object.
Excludes NA values by default.
Parameters
----------
dropna : boolean, default True
Don't include NaN in the count.
Returns
-------
nunique : int
"""
uniqs
=
self
.unique()
n
=
len
(uniqs)
if
dropna
and
isna(uniqs).any():
n
-=
1
return
n
.