Trả về DataFrame với các hàng trùng lặp bị xóa. Xem xét các cột nhất định là tùy chọn. Các chỉ mục, bao gồm các chỉ mục thời gian bị bỏ qua. Chỉ xem xét các cột nhất định để xác định các bản sao, theo mặc định, sử dụng tất cả các cột. Xác định những sao chép [nếu có] để giữ. - Có nên sửa đổi DataFrame thay vì tạo một cái mới. Nếu đúng, trục kết quả sẽ được dán nhãn 0, 1,,, n - 1. Mới trong phiên bản 1.0.0. DataFrame với các bản sao được xóa hoặc không có nếu Ví dụ Xem xét bộ dữ liệu chứa xếp hạng ramen.first
: Drop trùng lặp ngoại trừ lần xuất hiện đầu tiên. - last
: Drop trùng lặp ngoại trừ lần xuất hiện cuối cùng. - Sai: Thả tất cả các bản sao.inplace=True
.>>> df = pd.DataFrame[{
... 'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
... 'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
... 'rating': [4, 4, 3.5, 15, 5]
... }]
>>> df
brand style rating
0 Yum Yum cup 4.0
1 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
Theo mặc định, nó loại bỏ các hàng trùng lặp dựa trên tất cả các cột.
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.0
Để loại bỏ các bản sao trên [các] cột cụ thể, hãy sử dụng subset
.
>>> df.drop_duplicates[subset=['brand']] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5
Để loại bỏ các bản sao và giữ các lần xuất hiện cuối cùng, hãy sử dụng keep
.
>>> df.drop_duplicates[subset=['brand', 'style'], keep='last'] brand style rating 1 Yum Yum cup 4.0 2 Indomie cup 3.5 4 Indomie pack 5.0
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọcdrop_duplicates[] method helps in removing duplicates from the Pandas Dataframe In Python.
Bàn luận
Phương thức pandas drop_duplicates [] giúp loại bỏ các bản sao khỏi khung dữ liệu gấu trúc trong Python.DataFrame.drop_duplicates[subset=None, keep=’first’, inplace=False]
Parameters:
- Cú pháp của df.drop_duplicates []Subset takes a column or list of column label. It’s default value is none. After passing columns, it will consider them only for duplicates.
- Cú pháp: dataFrame.drop_duplicates [tập hợp con = khôngkeep is to control how to consider duplicate value. It has only three distinct value and default is ‘first’.
- Tập hợp con: Tập hợp con lấy một cột hoặc danh sách nhãn cột. Giá trị mặc định của nó là không có. Sau khi vượt qua các cột, nó sẽ chỉ xem xét chúng cho các bản sao. & NBSP;first‘, it considers first value as unique and rest of the same values as duplicate.
- Giữ: Giữ là kiểm soát cách xem xét giá trị trùng lặp. Nó chỉ có ba giá trị riêng biệt và mặc định là ‘đầu tiên. & NBSP;last‘, it considers last value as unique and rest of the same values as duplicate.
- Nếu ‘đầu tiên, nó coi giá trị đầu tiên là duy nhất và phần còn lại của các giá trị giống như trùng lặp.False, it consider all of the same values as duplicates
- Nếu ‘cuối cùng, nó coi giá trị cuối cùng là duy nhất và phần còn lại của các giá trị giống như trùng lặp.Boolean values, removes rows with duplicates if True.
Nếu sai, nó xem xét tất cả các giá trị giống như các bản saoDataFrame with removed duplicate rows depending on Arguments passed.
Example:
Thay thế: Các giá trị Boolean, loại bỏ các hàng với các bản sao nếu đúng.
Python3
Loại trả về: DataFrame với các hàng trùng lặp bị xóa tùy thuộc vào các đối số được truyền. & NBSP;
Như chúng ta có thể thấy một trong những đội và nhóm đã bị loại bỏ do giá trị trùng lặp.
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.00
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.01
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.05
>>> df.drop_duplicates[subset=['brand']] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.59
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.07
>>> df.drop_duplicates[subset=['brand', 'style'], keep='last'] brand style rating 1 Yum Yum cup 4.0 2 Indomie cup 3.5 4 Indomie pack 5.01
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.09
>>> df.drop_duplicates[subset=['brand', 'style'], keep='last'] brand style rating 1 Yum Yum cup 4.0 2 Indomie cup 3.5 4 Indomie pack 5.03
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.09
>>> df.drop_duplicates[subset=['brand', 'style'], keep='last'] brand style rating 1 Yum Yum cup 4.0 2 Indomie cup 3.5 4 Indomie pack 5.03
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.09
>>> df.drop_duplicates[subset=['brand', 'style'], keep='last'] brand style rating 1 Yum Yum cup 4.0 2 Indomie cup 3.5 4 Indomie pack 5.07
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.09
>>> df.drop_duplicates[subset=['brand', 'style'], keep='last'] brand style rating 1 Yum Yum cup 4.0 2 Indomie cup 3.5 4 Indomie pack 5.01
>>> df.drop_duplicates[subset=['brand']] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.57
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.05
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False2
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.07
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False4
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.09
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False6
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.09
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False6
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.09
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False6
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.09
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False4
first
3first
4
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.02
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.03
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.04
first
8
Output:
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False
Các
first
5>>> df.drop_duplicates[]
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
3 first
7Removing rows with the same First Name
Để tải xuống tệp CSV được sử dụng, bấm vào đây. & NBSP;
Python3
Loại trả về: DataFrame với các hàng trùng lặp bị xóa tùy thuộc vào các đối số được truyền. & NBSP;
Như chúng ta có thể thấy một trong những đội và nhóm đã bị loại bỏ do giá trị trùng lặp.
last
6last
7last
8
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.03
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False4
last
5inplace=True
2
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.03
last
7inplace=True
5inplace=True
6inplace=True
7
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.03
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False6
last
8>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.03
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False4
last
5subset
4
Output:
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.00
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.01
>>> df.drop_duplicates[]
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
2>>> df.drop_duplicates[]
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
3 >>> df.drop_duplicates[]
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
4Removing rows with all duplicate values
Các
Python3
first
5
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.03
first
7Để tải xuống tệp CSV được sử dụng, bấm vào đây. & NBSP;
keep
8keep
9
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.000
keep
5keep
6>>> df.drop_duplicates[subset=['brand']] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.57
keep
8keep
9
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.006
keep
5keep
6>>> df.drop_duplicates[subset=['brand']] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.57
keep
8keep
9
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.012
keep
5keep
6>>> df.drop_duplicates[subset=['brand']] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.57
keep
8keep
9
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.018
keep
5keep
6>>> df.drop_duplicates[subset=['brand']] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.57
keep
8keep
9
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.024
keep
5keep
6>>> df.drop_duplicates[subset=['brand']] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.57
keep
8keep
9
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.030
keep
5keep
6>>> df.drop_duplicates[subset=['brand']] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.57
keep
8keep
9
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.036
keep
5keep
6>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.039
first
5
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.03
first
7last
6last
7last
8
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.03
A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False4
last
5Để tải xuống tệp CSV được sử dụng, bấm vào đây. & NBSP;
Ví dụ 1: Loại bỏ các hàng có cùng tên & nbsp;
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.061
>>> df.drop_duplicates[] brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.062
Output:
Trong ví dụ sau, các hàng có cùng tên được xóa và khung dữ liệu mới được trả về.