Pandas统计重复的列里面的值

本文介绍使用 Pandas 库进行数据处理时如何查找并去除重复数据,包括按特定列去重的方法,并提供了多种实现方式及示例代码。

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

pandas

代码如下:

import pandas as pd
import numpy as np

salaries = pd.DataFrame({
    'name': ['BOSS', 'Lilei', 'Lilei', 'Han', 'BOSS', 'BOSS', 'Han', 'BOSS'],
    'Year': [2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017],
    'Salary': [1, 2, 3, 4, 5, 6, 7, 8],
    'Bonus': [2, 2, 2, 2, 3, 4, 5, 6]
})
print(salaries)
print(salaries['Bonus'].duplicated(keep='first'))
print(salaries[salaries['Bonus'].duplicated(keep='first')].index)
print(salaries[salaries['Bonus'].duplicated(keep='first')])
print(salaries['Bonus'].duplicated(keep='last'))
print(salaries[salaries['Bonus'].duplicated(keep='last')].index)
print(salaries[salaries['Bonus'].duplicated(keep='last')])

输出如下:

   Bonus  Salary  Year   name
0      2       1  2016   BOSS
1      2       2  2016  Lilei
2      2       3  2016  Lilei
3      2       4  2016    Han
4      3       5  2017   BOSS
5      4       6  2017   BOSS
6      5       7  2017    Han
7      6       8  2017   BOSS
0    False
1     True
2     True
3     True
4    False
5    False
6    False
7    False
Name: Bonus, dtype: bool
Int64Index([1, 2, 3], dtype='int64')
   Bonus  Salary  Year   name
1      2       2  2016  Lilei
2      2       3  2016  Lilei
3      2       4  2016    Han
0     True
1     True
2     True
3    False
4    False
5    False
6    False
7    False
Name: Bonus, dtype: bool
Int64Index([0, 1, 2], dtype='int64')
   Bonus  Salary  Year   name
0      2       1  2016   BOSS
1      2       2  2016  Lilei
2      2       3  2016  Lilei

非pandas

对于如nunpy中的这些操作主要如下:
假设有数组
a = np.array([1, 2, 1, 3, 3, 3, 0])
想找出 [1 3]
则有

方法1
m = np.zeros_like(a, dtype=bool)
m[np.unique(a, return_index=True)[1]] = True
a[~m]
方法2
a[~np.in1d(np.arange(len(a)), np.unique(a, return_index=True)[1], assume_unique=True)]
方法3
np.setxor1d(a, np.unique(a), assume_unique=True)
方法4
u, i = np.unique(a, return_inverse=True)
u[np.bincount(i) > 1]
方法5
s = np.sort(a, axis=None)
s[:-1][s[1:] == s[:-1]]

参考:https://stackoverflow.com/questions/11528078/determining-duplicate-values-in-an-array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值