pandas统计重复值次数

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
    原文作者:Mr番茄蛋
    原文地址: https://blog.csdn.net/qq_35203425/article/details/80830911
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞