Bird
Raised Fist0
Pandasdata~5 mins

Duplicates on specific columns in Pandas - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the pandas function duplicated() do when used on a DataFrame?
It returns a Boolean Series indicating whether each row is a duplicate of a previous row. By default, it checks all columns.
Click to reveal answer
beginner
How can you check for duplicates only on specific columns in a pandas DataFrame?
Use df.duplicated(subset=[column_names]) where column_names is a list of columns to check for duplicates.
Click to reveal answer
beginner
What does the parameter keep='first' do in duplicated()?
It marks all duplicates as True except for the first occurrence, which is marked as False.
Click to reveal answer
beginner
How do you remove duplicate rows based on specific columns in pandas?
Use df.drop_duplicates(subset=[column_names]) to keep only the first occurrence of duplicates in those columns.
Click to reveal answer
beginner
Why might you want to check duplicates on specific columns instead of the whole DataFrame?
Because sometimes only certain columns define the uniqueness of a row, and other columns may have different data but still represent the same entity.
Click to reveal answer
Which pandas method helps identify duplicate rows based on specific columns?
Afillna()
Bdropna()
Cgroupby()
Dduplicated(subset=[...])
What does df.duplicated(subset=['col1', 'col2'], keep='last') do?
AMarks all duplicates except the last occurrence as True
BMarks all duplicates except the first occurrence as True
CRemoves duplicates from the DataFrame
DChecks duplicates on all columns
How do you remove duplicate rows based on columns 'A' and 'B' in pandas?
Adf.duplicated(['A', 'B'])
Bdf.dropna(['A', 'B'])
Cdf.drop_duplicates(subset=['A', 'B'])
Ddf.fillna(['A', 'B'])
If you want to find duplicates considering all columns, what should you do?
ACall duplicated() without subset parameter
BUse duplicated(subset=[])
CUse drop_duplicates(subset=[])
DUse duplicated(subset=None)
Why is it useful to specify columns in the subset parameter when checking duplicates?
ATo sort the DataFrame
BTo focus on columns that define uniqueness
CTo change data types
DTo speed up the DataFrame
Explain how to find and remove duplicate rows based on specific columns in a pandas DataFrame.
Think about how to tell pandas which columns to check for duplicates.
You got /3 concepts.
    Why might checking duplicates on the entire DataFrame be less useful than checking on specific columns?
    Consider real-life examples where only some details matter for identifying duplicates.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the subset parameter do in the duplicated() method in pandas?
      easy
      A. It specifies which columns to check for duplicates.
      B. It removes duplicates from the DataFrame.
      C. It sorts the DataFrame before checking duplicates.
      D. It changes the data type of columns before checking duplicates.

      Solution

      1. Step 1: Understand the purpose of duplicated()

        The duplicated() method identifies duplicate rows in a DataFrame.
      2. Step 2: Role of subset parameter

        The subset parameter limits the duplicate check to specific columns instead of all columns.
      3. Final Answer:

        It specifies which columns to check for duplicates. -> Option A
      4. Quick Check:

        subset = columns to check duplicates [OK]
      Hint: Use subset to focus duplicate check on chosen columns [OK]
      Common Mistakes:
      • Thinking subset removes duplicates
      • Confusing subset with sorting
      • Assuming subset changes data types
      2. Which of the following is the correct syntax to find duplicates based on columns 'A' and 'B' in a pandas DataFrame df?
      easy
      A. df.duplicated(subset=['A', 'B'])
      B. df.duplicates(columns=['A', 'B'])
      C. df.duplicated(columns=['A', 'B'])
      D. df.find_duplicates(['A', 'B'])

      Solution

      1. Step 1: Recall pandas method for duplicates

        The correct method is duplicated() to find duplicate rows.
      2. Step 2: Correct use of subset parameter

        The subset parameter takes a list of column names to check duplicates on.
      3. Final Answer:

        df.duplicated(subset=['A', 'B']) -> Option A
      4. Quick Check:

        Correct method and subset syntax [OK]
      Hint: Use duplicated(subset=[...]) to specify columns [OK]
      Common Mistakes:
      • Using non-existent methods like duplicates()
      • Passing columns instead of subset
      • Wrong method names like find_duplicates()
      3. Given the DataFrame:
      import pandas as pd
      data = {'A': [1, 2, 1, 3], 'B': [4, 5, 4, 6], 'C': [7, 8, 9, 10]}
      df = pd.DataFrame(data)
      print(df.duplicated(subset=['A', 'B']))

      What is the output?
      medium
      A. [False, True, False, False]
      B. [True, False, True, False]
      C. [False, False, True, False]
      D. [False, False, False, True]

      Solution

      1. Step 1: Understand the DataFrame and subset

        The DataFrame has rows with columns A and B: (1,4), (2,5), (1,4), (3,6).
      2. Step 2: Identify duplicates on columns 'A' and 'B'

        The third row (index 2) duplicates the first row (index 0) on columns A and B.
      3. Final Answer:

        [False, False, True, False] -> Option C
      4. Quick Check:

        Only third row is duplicate on A and B [OK]
      Hint: Check repeated (A,B) pairs for True in duplicated() output [OK]
      Common Mistakes:
      • Assuming duplicates on all columns
      • Marking first occurrence as duplicate
      • Confusing index positions
      4. You run this code:
      df.duplicated(subset=['A', 'B'], keep='last')

      but get unexpected results. What is a common mistake causing this?
      medium
      A. Forgetting to pass a list to subset causes an error.
      B. Using keep='last' marks the last occurrence as False, others as True.
      C. Using keep='last' marks the first duplicates as True, not the last.
      D. The duplicated() method does not accept keep parameter.

      Solution

      1. Step 1: Understand keep parameter behavior

        keep='last' marks all duplicates except the last occurrence as True.
      2. Step 2: Identify common confusion

        Many expect it to mark the last duplicate as True, but it actually keeps the last as False (not duplicate).
      3. Final Answer:

        Using keep='last' marks the last occurrence as False, others as True. -> Option B
      4. Quick Check:

        keep='last' keeps last duplicate False [OK]
      Hint: Remember keep='last' keeps last duplicate False, others True [OK]
      Common Mistakes:
      • Thinking keep='last' marks last duplicate True
      • Passing subset as string instead of list
      • Believing duplicated() lacks keep parameter
      5. You have a DataFrame df with columns 'Name', 'Age', 'City'. You want to remove rows where 'Name' and 'City' are duplicates but keep the first occurrence. Which code correctly does this?
      hard
      A. df[~df.duplicated(subset=['Name', 'City'], keep='first')]
      B. df = df.duplicated(subset=['Name', 'City'], keep='first')
      C. df = df.drop_duplicates(columns=['Name', 'City'], keep='last')
      D. df = df.drop_duplicates(subset=['Name', 'City'], keep='first')

      Solution

      1. Step 1: Identify method to remove duplicates

        Use drop_duplicates() to remove duplicate rows.
      2. Step 2: Use subset and keep parameters correctly

        Pass subset=['Name', 'City'] and keep='first' to keep first occurrence and remove others.
      3. Step 3: Check options

        df = df.drop_duplicates(subset=['Name', 'City'], keep='first') uses correct method and parameters. df[~df.duplicated(subset=['Name', 'City'], keep='first')] uses duplicated() with negation but does not assign back to df. df = df.drop_duplicates(columns=['Name', 'City'], keep='last') uses wrong parameter name. df = df.duplicated(subset=['Name', 'City'], keep='first') returns boolean Series, not DataFrame.
      4. Final Answer:

        df = df.drop_duplicates(subset=['Name', 'City'], keep='first') -> Option D
      5. Quick Check:

        drop_duplicates with subset and keep='first' removes duplicates [OK]
      Hint: Use drop_duplicates(subset=[...], keep='first') to remove duplicates [OK]
      Common Mistakes:
      • Using duplicated() instead of drop_duplicates() to remove rows
      • Passing columns instead of subset
      • Not assigning result back to df
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard