Làm cách nào để kiểm tra xem có trùng lặp trong cột python không?

Pandas Hàm


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
8 được sử dụng để lấy/tìm/chọn danh sách tất cả các hàng trùng lặp [tất cả hoặc các cột được chọn] từ pandas. Hàng trùng lặp có nghĩa là có nhiều hàng trên tất cả các cột. Sử dụng phương pháp này, bạn có thể nhận được các hàng trùng lặp trên nhiều cột hoặc tất cả các cột đã chọn. Trong bài viết này, tôi sẽ giải thích những điều này với một số ví dụ

1. Ví dụ nhanh về Lấy danh sách tất cả các mục trùng lặp

Nếu bạn đang vội, dưới đây là một số ví dụ nhanh về cách lấy danh sách tất cả các hàng trùng lặp trong DataFrame của gấu trúc


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]

Bây giờ, hãy tạo một DataFrame với một vài hàng trùng lặp trên tất cả các cột. Khung dữ liệu của chúng tôi chứa các tên cột


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
9,

   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
0,

   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
1 và

   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
2


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]

Sản lượng dưới sản lượng


   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000

2. Chọn các hàng trùng lặp dựa trên tất cả các cột

Bạn có thể sử dụng


   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
3 mà không cần bất kỳ đối số nào để nhận các hàng có cùng giá trị trên tất cả các cột. Nó nhận các giá trị mặc định là

   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
4 và

   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
5. Ví dụ dưới đây trả về hai hàng vì đây là các hàng trùng lặp trong DataFrame của chúng tôi


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]
1

Sản lượng dưới sản lượng


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]
2

Bạn có thể đặt


   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
6 trong chức năng trùng lặp để nhận tất cả các mục trùng lặp mà không loại bỏ các hàng trùng lặp


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]
4

Sản lượng dưới sản lượng


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]
5

3. Nhận danh sách các hàng cuối cùng trùng lặp dựa trên tất cả các cột

Bạn muốn chọn tất cả các hàng trùng lặp ngoại trừ lần xuất hiện cuối cùng của chúng, chúng ta phải truyền đối số


   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
7 là

   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
8. Ví dụ,

   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   pandas  30000   50days      2000
4   Python  22000   40days      2300
5    Spark  20000   30days      1000
6   pandas  30000   50days      2000
9


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]
9

Sản lượng dưới sản lượng


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
0

4. Nhận danh sách các hàng trùng lặp bằng các cột đơn

Bạn muốn chọn các hàng trùng lặp dựa trên các cột đơn lẻ, sau đó chuyển tên cột làm đối số


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
1

Sản lượng dưới sản lượng


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
0

5. Nhận danh sách các hàng trùng lặp bằng nhiều cột

Để nhận/tìm các hàng trùng lặp trên cơ sở nhiều cột, hãy chỉ định tất cả các tên cột dưới dạng danh sách


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
1

Sản lượng dưới sản lượng


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]
2

6. Nhận danh sách các hàng trùng lặp bằng cách sử dụng các giá trị sắp xếp

Hãy xem cách sắp xếp kết quả của phương thức Duplicate[]. Bạn có thể sắp xếp pandas DataFrame theo một hoặc nhiều [một hoặc nhiều] cột bằng phương pháp


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]
10


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
3

Sản lượng dưới sản lượng


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
4

Thay vào đó, bạn có thể sử dụng


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]
11 để sắp xếp sau bộ lọc trùng lặp


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
5

Sản lượng dưới sản lượng


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
6

7. Hoàn thành ví dụ để lấy danh sách tất cả các mục trùng lặp


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas","Python","Spark","pandas"],
    'Fee' :[20000,25000,22000,30000,22000,20000,30000],
    'Duration':['30days','40days','35days','50days','40days','30days','50days'],
    'Discount':[1000,2300,1200,2000,2300,1000,2000]
              }
df = pd.DataFrame[technologies]
print[df]
7

Sự kết luận

Trong bài viết này, bạn đã học cách lấy/chọn danh sách tất cả các hàng trùng lặp [tất cả hoặc nhiều cột] bằng phương pháp pandas DataFrame


# Below are quick example
# Select duplicate rows except first occurrence based on all columns
df2 = df[df.duplicated[]]

# Select duplicate row based on all columns
df2 = df[df.duplicated[keep=False]]

# Get duplicate last rows based on all columns
df2 = df[df.duplicated[keep = 'last']]

# Get list Of duplicate rows using single columns
df2 = df[df['Courses'].duplicated[] == True]

# Get list of duplicate rows based on 'Courses' column
df2 = df[df.duplicated['Courses']]

# Get list Of duplicate rows using multiple columns
df2 = df[df[['Courses', 'Fee','Duration']].duplicated[] == True]

# Get list of duplicate rows based on list of column names
df2 = df[df.duplicated[['Courses','Fee','Duration']]]
12 với các ví dụ

Chủ Đề