Bird
Raised Fist0
Pandasdata~10 mins

Why systematic cleaning matters in Pandas - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load the CSV file into a DataFrame.

Pandas
import pandas as pd
df = pd.[1]('data.csv')
Drag options to blanks, or click blank then click option'
Aread_csv
Bread_excel
Cto_csv
DDataFrame
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'to_csv' which saves data instead of loading it.
Using 'read_excel' which is for Excel files.
2fill in blank
medium

Complete the code to remove rows with missing values.

Pandas
clean_df = df.[1]()
Drag options to blanks, or click blank then click option'
Aisnull
Bfillna
Cdropna
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fillna' which fills missing values instead of removing rows.
Using 'isnull' which checks for missing values but does not remove rows.
3fill in blank
hard

Fix the error in the code to convert a column to lowercase.

Pandas
df['Name'] = df['Name'].[1]()
Drag options to blanks, or click blank then click option'
Alower
Btitle
Ccapitalize
Dupper
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'upper' which converts to uppercase.
Using 'capitalize' which only changes the first letter.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.

Pandas
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using 'word' instead of 'len(word)' as dictionary value.
5fill in blank
hard

Fill all three blanks to create a filtered dictionary with uppercase keys and values greater than 0.

Pandas
result = [1]: [2] for k, v in data.items() if v [3] 0}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k.lower()' instead of 'k.upper()' for keys.
Using '<' instead of '>' in the condition.
Using 'k' instead of 'v' as dictionary values.

Practice

(1/5)
1. Why is systematic cleaning important when working with data in pandas?
easy
A. It increases the file size of the dataset.
B. It makes the data look colorful and pretty.
C. It helps ensure the analysis results are accurate and reliable.
D. It automatically creates new data columns.

Solution

  1. Step 1: Understand the purpose of cleaning data

    Cleaning removes errors and inconsistencies that can mislead analysis.
  2. Step 2: Connect cleaning to analysis trustworthiness

    Systematic cleaning ensures results are based on correct and consistent data.
  3. Final Answer:

    It helps ensure the analysis results are accurate and reliable. -> Option C
  4. Quick Check:

    Cleaning = Accurate results [OK]
Hint: Cleaning fixes errors to trust your results [OK]
Common Mistakes:
  • Thinking cleaning only changes data appearance
  • Believing cleaning adds new data automatically
  • Assuming cleaning increases data size
2. Which pandas function is commonly used to remove rows with missing values?
easy
A. df.replace()
B. df.dropna()
C. df.drop_duplicates()
D. df.fillna()

Solution

  1. Step 1: Identify function to remove missing data

    df.dropna() removes rows with any missing (NaN) values.
  2. Step 2: Differentiate from other functions

    df.fillna() fills missing values, drop_duplicates() removes duplicate rows, replace() changes values.
  3. Final Answer:

    df.dropna() -> Option B
  4. Quick Check:

    Remove missing rows = dropna() [OK]
Hint: dropna removes missing rows, fillna fills them [OK]
Common Mistakes:
  • Confusing fillna() with dropna()
  • Using drop_duplicates() to remove missing values
  • Trying replace() to drop rows
3. Given the code below, what will be the output?
import pandas as pd
data = {'Name': ['Anna', 'Bob', None, 'Diana'], 'Age': [25, None, 30, 22]}
df = pd.DataFrame(data)
df_clean = df.dropna()
print(df_clean)
medium
A. DataFrame with only rows where 'Name' and 'Age' are not None
B. DataFrame with all rows including those with None values
C. Empty DataFrame with no rows
D. Error because dropna() needs parameters

Solution

  1. Step 1: Understand dropna() behavior

    dropna() removes rows with any missing (None/NaN) values in any column.
  2. Step 2: Apply dropna() to given data

    Rows with None in 'Name' or 'Age' are removed, leaving only rows with complete data.
  3. Final Answer:

    DataFrame with only rows where 'Name' and 'Age' are not None -> Option A
  4. Quick Check:

    dropna() removes rows with missing values [OK]
Hint: dropna() keeps only complete rows [OK]
Common Mistakes:
  • Expecting dropna() to fill missing values
  • Thinking dropna() removes columns instead of rows
  • Assuming dropna() causes an error without parameters
4. The following code is intended to remove duplicate rows from a DataFrame, but it does not work as expected. What is the error?
import pandas as pd
data = {'A': [1, 2, 2, 3], 'B': [4, 5, 5, 6]}
df = pd.DataFrame(data)
df.drop_duplicates()
print(df)
medium
A. drop_duplicates() cannot be used on numeric columns.
B. drop_duplicates() requires a column name as argument.
C. drop_duplicates() needs to be called before creating the DataFrame.
D. drop_duplicates() does not remove duplicates without inplace=True or reassignment.

Solution

  1. Step 1: Check drop_duplicates() usage

    By default, drop_duplicates() returns a new DataFrame and does not change the original unless inplace=True or reassigned.
  2. Step 2: Identify why duplicates remain

    Since df.drop_duplicates() is called without assignment or inplace=True, df remains unchanged.
  3. Final Answer:

    drop_duplicates() does not remove duplicates without inplace=True or reassignment. -> Option D
  4. Quick Check:

    drop_duplicates() needs inplace or assignment [OK]
Hint: Use inplace=True or assign result to remove duplicates [OK]
Common Mistakes:
  • Assuming drop_duplicates() modifies original DataFrame by default
  • Thinking drop_duplicates() needs column argument always
  • Believing drop_duplicates() cannot work on numbers
5. You have a DataFrame with messy text data in a column 'City' that contains extra spaces and inconsistent capitalization. Which sequence of pandas operations best cleans this column systematically?
hard
A. df['City'] = df['City'].str.strip().str.title() # No further cleaning needed
B. df['City'] = df['City'].str.strip().str.title() df['City'] = df['City'].str.replace(' ', '')
C. df['City'] = df['City'].str.strip().str.title() df['City'] = df['City'].str.replace(' ', ' ') df['City'] = df['City'].str.replace(' ', ' ')
D. df['City'] = df['City'].str.strip().str.lower() df['City'] = df['City'].str.capitalize() df['City'] = df['City'].str.replace(' ', '')

Solution

  1. Step 1: Remove extra spaces

    str.strip() removes leading and trailing spaces effectively.
  2. Step 2: Fix capitalization

    str.title() converts text to title case, making capitalization consistent.
  3. Step 3: Avoid unnecessary replacements

    Removing all spaces or replacing spaces incorrectly can harm data integrity.
  4. Final Answer:

    df['City'] = df['City'].str.strip().str.title() -> Option A
  5. Quick Check:

    Strip spaces + title case = clean text [OK]
Hint: Strip spaces then title case for clean text [OK]
Common Mistakes:
  • Removing all spaces inside city names
  • Applying inconsistent capitalization methods
  • Overcomplicating cleaning with unnecessary replacements
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