Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.Series.drop_duplicates#

Series.drop_duplicates(*,keep='first',inplace=False,ignore_index=False)[source]#

Return Series with duplicate values removed.

Parameters:
keep{‘first’, ‘last’,False}, default ‘first’

Method to handle dropping duplicates:

  • ‘first’ : Drop duplicates except for the first occurrence.

  • ‘last’ : Drop duplicates except for the last occurrence.

  • False : Drop all duplicates.

inplacebool, defaultFalse

IfTrue, performs operation inplace and returns None.

ignore_indexbool, defaultFalse

IfTrue, the resulting axis will be labeled 0, 1, …, n - 1.

Added in version 2.0.0.

Returns:
Series or None

Series with duplicates dropped or None ifinplace=True.

See also

Index.drop_duplicates

Equivalent method on Index.

DataFrame.drop_duplicates

Equivalent method on DataFrame.

Series.duplicated

Related method on Series, indicating duplicate Series values.

Series.unique

Return unique values as an array.

Examples

Generate a Series with duplicated entries.

>>>s=pd.Series(['llama','cow','llama','beetle','llama','hippo'],...name='animal')>>>s0     llama1       cow2     llama3    beetle4     llama5     hippoName: animal, dtype: object

With the ‘keep’ parameter, the selection behaviour of duplicated valuescan be changed. The value ‘first’ keeps the first occurrence for eachset of duplicated entries. The default value of keep is ‘first’.

>>>s.drop_duplicates()0     llama1       cow3    beetle5     hippoName: animal, dtype: object

The value ‘last’ for parameter ‘keep’ keeps the last occurrence foreach set of duplicated entries.

>>>s.drop_duplicates(keep='last')1       cow3    beetle4     llama5     hippoName: animal, dtype: object

The valueFalse for parameter ‘keep’ discards all sets ofduplicated entries.

>>>s.drop_duplicates(keep=False)1       cow3    beetle5     hippoName: animal, dtype: object

[8]ページ先頭

©2009-2025 Movatter.jp