pandas 某一列不重复的值有多少

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



以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

Don’t include NaN in the count.

Returns:
nunique : int

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

.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值