unique()与nunique()

1 unique()

统计list、series中的不同值的个数

2 nunique()

可直接统计dataframe中每列的不同值的个数,也可用于series,但不能用于list.

df=pd.DataFrame({'A':[0,1,1],'B':[0,5,6]})
print(df)
print(df.nunique())
#    A  B
# 0  0  0
# 1  1  5
# 2  1  6
# A    2
# B    3
# dtype: int64

也可与groupby结合使用,统计每个块的不同值的个数.

all_user_repay = all_user_repay.groupby(['user_id'])['listing_id'].agg(['nunique']).reset_index()
#    user_id  nunique
# 0       40        1
# 1       56        1
# 2       98        1
# 3      103        1
# 4      122        1

猜你喜欢

转载自www.cnblogs.com/xxswkl/p/11009059.html