以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
.
本文通过pandas的groupby和nunique方法,以o2o大赛数据为例,展示了如何统计DataFrame中'User_id'对应'Merchant_id'的不重复值数量。同时,还使用了value_counts和unique方法来获取具体不重复的'Merchant_id'和了解返回唯一值的详情。

3万+

被折叠的 条评论
为什么被折叠?



