本文主要介绍了pandas统计重复值次数的方法实现,分享给大家,具体如下:
from pandas import DataFrame df = DataFrame({'key1':['a','a','b','b','a','a'], 'key2':['one','two','one','two','one','one'], 'data1':[1,2,3,2,1,1], # 'data2':np.random.randn(5) }) # 打印数据框 print(df) # data1 key1 key2 # 0 1 a one # 1 2 a two # 2 3 b one # 3 2 b two # 4 1 a one # 5 1 a one # 重复项 print(df[df.duplicated()]) # data1 key1 key2 # 4 1 a one # 5 1 a one # 统计重复值 dup=df[df.duplicated()].count() print(dup) # 最后两项重复 # data1 2 # key1 2 # key2 2 # 去除重复项 nodup=df[-df.duplicated()] print(nodup) # data1 key1 key2 # 0 1 a one # 1 2 a two # 2 3 b one # 3 2 b two
pandas 中 dataframe 重复元素个数的获取
方法有二:
1. 在调用duplicated方法后,非重复的元素会被标记为False,而重复的元素会被标记为True
count = 0 for i in users_info['user_id'].duplicated(): if i == True: count = count + 1 count
【注1】users_info为一个dataframe框,user_id为其中一列
【注2】duplicated( )方法只会把重复的元素标记为True,而不会标记被重复的元素
2.这行代码的速度更快,drop_duplicates(['user_id'])方法为删除user_id列中相同的元素
users_info.shape[0] - users_info.drop_duplicates(['user_id']).shape[0]
【注】shape[0] 为获取行数