Pandas 的 DataFrame.drop_duplicates(~)
方法返回刪除了重複行的 DataFrame。
參數
1.subset
| string
或 list
| optional
用於識別重複項的列。默認情況下,使用所有列。
2. keep
| string
或 boolean
| optional
如何處理重複行:
值 |
意義 |
---|---|
|
僅保留第一個出現的位置,並刪除其餘的位置。 |
|
僅保留最後一個出現的位置,並刪除第一個出現的位置。 |
|
刪除所有重複項。 |
默認情況下,keep="first"
。
3. inplace
| boolean
| optional
-
如果是
True
,那麽該方法將直接修改源DataFrame,而不是創建新的DataFrame。 -
如果是
False
,則將創建並返回一個新的DataFrame。
默認情況下,inplace=False
。
4. ignore_index
| boolean
| optional
-
如果
True
,則返回的 DataFrame 將具有索引標簽0
、1
、 ...、n-1
,其中n
是返回的 DataFrame 的行數。 -
如果
False
,則返回的 DataFrame 將保留其原始索引。
默認情況下,ignore_index=False
。
返回值
根據指定參數刪除重複行的DataFrame
。如果是 inplace=True
,則不會返回任何內容,因為源 DataFrame 被直接修改。
例子
基本用法
考慮以下 DataFrame :
df = pd.DataFrame({"A":[2,2,2], "B":[4,5,4], "C":[2,8,2]})
df
A B C
0 2 4 2
1 2 5 8
2 2 4 2
第一行和第三行是重複的。要刪除重複的行:
df.drop_duplicates()
A B C
0 2 4 2
1 2 5 8
刪除重複的行,僅考慮某些列
考慮以下 DataFrame :
df = pd.DataFrame({"A":[2,2], "B":[4,3], "C":[2,2]})
df
A B C
0 2 4 2
1 2 3 2
在這裏,第一行和第二行並不完全重複( [2,4,2]
與 [2,3,2]
)。我們仍然可以通過指定 subset=["A","C"]
將它們聲明為重複項,這在檢查重複項時隻會考慮列 A
和 C
:
df.drop_duplicates(subset=["A","C"])
A B C
0 2 4 2
處理重複項的不同方法
正如 keep="first" || "last" || "False"
所指定的,我們可以通過三種方式處理重複項。
考慮以下 DataFrame :
df = pd.DataFrame({"A":[2,9,2], "B":[4,9,4], "C":[2,9,2]})
df
A B C
0 2 4 2
1 9 9 9
2 2 4 2
隻保留第一次出現的情況
df.drop_duplicates(keep="first") # This is the default behaviour
A B C
0 2 4 2
1 9 9 9
請注意第一次出現的重複項是如何保持原樣的。
隻保留最後一次出現的情況
df.drop_duplicates(keep="last")
A B C
1 9 9 9
2 2 4 2
請注意最後一次出現的重複項是如何保持原樣的。
刪除所有出現的情況
df.drop_duplicates(keep=False)
A B C
1 9 9 9
請注意所有重複項是如何被刪除的。
就地刪除重複行
要就地刪除重複行,請設置 inplace=True
。這將直接從源 DataFrame 中刪除重複行,而不是創建新行。
考慮以下 DataFrame ;
df = pd.DataFrame({"A":[2,9,2], "B":[4,9,4], "C":[2,9,2]})
df
A B C
0 2 4 2
1 9 9 9
2 2 4 2
我們使用 inplace=True
刪除所有重複行:
df.drop_duplicates(inplace=True)
df
A B C
0 2 4 2
1 9 9 9
如輸出所示,源 DataFrame 已被修改。
相關用法
- Python PySpark DataFrame drop方法用法及代碼示例
- Python PySpark DataFrame dropDuplicates方法用法及代碼示例
- Python Pandas DataFrame drop方法用法及代碼示例
- Python Pandas DataFrame droplevel方法用法及代碼示例
- Python PySpark DataFrame dropna方法用法及代碼示例
- Python Pandas DataFrame dropna方法用法及代碼示例
- Python PySpark DataFrame dtypes屬性用法及代碼示例
- Python Pandas DataFrame dtypes屬性用法及代碼示例
- Python Pandas DataFrame duplicated方法用法及代碼示例
- Python Pandas DataFrame diff方法用法及代碼示例
- Python Pandas DataFrame dot方法用法及代碼示例
- Python Pandas DataFrame describe方法用法及代碼示例
- Python PySpark DataFrame describe方法用法及代碼示例
- Python Pandas DataFrame div方法用法及代碼示例
- Python PySpark DataFrame distinct方法用法及代碼示例
- Python Pandas DataFrame empty屬性用法及代碼示例
- Python Pandas DataFrame pop方法用法及代碼示例
- Python Pandas DataFrame nsmallest方法用法及代碼示例
- Python Pandas DataFrame sample方法用法及代碼示例
- Python Pandas DataFrame items方法用法及代碼示例
- Python Pandas DataFrame max方法用法及代碼示例
- Python Pandas DataFrame swaplevel方法用法及代碼示例
- Python Pandas DataFrame agg方法用法及代碼示例
- Python Pandas DataFrame copy方法用法及代碼示例
- Python Pandas DataFrame pow方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas DataFrame | drop_duplicates method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。