DataFrame.duplicated
(self, subset: Union[Hashable, Sequence[Hashable], NoneType] = None, keep: Union[str, bool] = ‘first’) → ‘Series’
- subset: 列标签(表头),可以是单个,也可以是列表
- keep=’first’(默认):标记/删除除第一个匹配项以外的重复项。
- keep=“last”:标记/删除除最后一个匹配项以外的重复项。
- keep=False:标记/删除所有重复项。
- return : 返回一个断subset所在行是不是重复行的Boolean Series
DataFrame.drop_duplicates
(self, subset: Union[Hashable, Sequence[Hashable], NoneType] = None, keep: Union[str, bool] = ‘first’, inplace: bool = False, ignore_index: bool = False)
- subset: 列标签(表头),可以是单个,也可以是列表
- keep=’first’(默认):标记/删除除第一个匹配项以外的重复项。
- keep=“last”:标记/删除除最后一个匹配项以外的重复项。
- keep=False:标记/删除所有重复项。
- inplace :是否更改源数据
- ingore_index:是否忽略index
- return 返回一个DataFrame
例子
>>> df2 = pd.DataFrame({'a': ['one', 'one', 'two', 'two', 'two', 'three', 'four'],
... 'b': ['x', 'y', 'x', 'y', 'x', 'x', 'x'],
... 'c': np.random.randn(7)})
>>> df2
a b c
0 one x 0.362045
1 one y -0.451623
2 two x 1.035298
3 two y -0.454305
4 two x -0.660347
5 three x 1.097390
6 four x 0.883549
>>> df2.duplicated('a')
0 False
1 True
2 False
3 True
4 True
5 False
6 False
dtype: bool
>>> df2.duplicated('a', keep='last')
0 True
1 False
2 True
3 True
4 False
5 False
6 False
dtype: bool
>>> df2.duplicated('a', keep=False)
0 True
1 True
2 True
3 True
4 True
5 False
6 False
dtype: bool
>>> df2.drop_duplicates('a')
a b c
0 one x 0.362045
2 two x 1.035298
5 three x 1.097390
6 four x 0.883549
>>> df2.drop_duplicates('a', keep='last')
a b c
1 one y -0.451623
4 two x -0.660347
5 three x 1.097390
6 four x 0.883549
>>> df2.drop_duplicates('a', keep=False)
a b c
5 three x 1.097390
6 four x 0.883549
1 Comment
Pandas – Indexing and selecting data – 心自彷徨的窝窝~ · 2020年3月25日 at 13:53
[…] duplicated 数据处理见链接 pandas DataFrame 重复数据处理 – duplicated()和 drop_duplicates() […]