Bird
Raised Fist0
Pandasdata~20 mins

Standardizing column names in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Column Name Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code after standardizing column names?

Given a DataFrame with columns having spaces and uppercase letters, what will be the column names after applying the standardization code?

Pandas
import pandas as pd

df = pd.DataFrame(columns=['First Name', 'Last Name', 'Age'])
df.columns = df.columns.str.strip().str.lower().str.replace(' ', '_')
print(list(df.columns))
A['first name', 'last name', 'age']
B['First_Name', 'Last_Name', 'Age']
C['first_name', 'last_name', 'age']
D['FIRST_NAME', 'LAST_NAME', 'AGE']
Attempts:
2 left
💡 Hint

Think about what each string method does: strip(), lower(), and replace().

❓ data_output
intermediate
2:00remaining
How many columns remain after filtering standardized columns?

After standardizing column names, you want to keep only columns that start with 'user_'. How many columns remain?

Pandas
import pandas as pd

df = pd.DataFrame(columns=['User ID', 'User Name', 'Age', 'user_email'])
df.columns = df.columns.str.strip().str.lower().str.replace(' ', '_')
filtered_cols = [col for col in df.columns if col.startswith('user_')]
print(len(filtered_cols))
A2
B4
C1
D3
Attempts:
2 left
💡 Hint

Check which columns start exactly with 'user_' after standardization.

🔧 Debug
advanced
2:00remaining
What error does this code raise when standardizing columns?

Identify the error raised by this code snippet:

Pandas
import pandas as pd

df = pd.DataFrame(columns=['Name', 'Age'])
df.columns = df.columns.str.lower().replace(' ', '_')
AAttributeError: 'Index' object has no attribute 'replace'
BSyntaxError: invalid syntax
CNo error, columns are standardized correctly
DTypeError: replace() missing 1 required positional argument
Attempts:
2 left
💡 Hint

Remember that str.lower() returns an Index object with string methods available, but replace is a string method, not an Index method.

🚀 Application
advanced
2:00remaining
Which option correctly standardizes columns to lowercase with underscores?

Choose the code snippet that correctly standardizes DataFrame columns by making them lowercase and replacing spaces with underscores.

Adf.columns = df.columns.str.replace(' ', '_').lower()
Bdf.columns = df.columns.str.lower().str.replace(' ', '_')
Cdf.columns = df.columns.replace(' ', '_').lower()
Ddf.columns = df.columns.str.lower().replace(' ', '_')
Attempts:
2 left
💡 Hint

Check which methods are available on the Index object and how to chain them.

🧠 Conceptual
expert
2:00remaining
Why is standardizing column names important in data science projects?

Choose the best reason why standardizing column names is a crucial step in data science workflows.

AIt ensures consistent column naming to avoid errors in code and makes data easier to understand and merge.
BIt increases the size of the dataset for better model training.
CIt automatically fixes missing values in the dataset.
DIt encrypts the column names to protect sensitive data.
Attempts:
2 left
💡 Hint

Think about how consistent naming helps when writing code and sharing data.

Practice

(1/5)
1. Why is it important to standardize column names in a pandas DataFrame?
easy
A. To increase the size of the DataFrame
B. To change the data types of columns automatically
C. To make data easier to work with and reduce errors in code
D. To delete duplicate rows in the DataFrame

Solution

  1. Step 1: Understand the purpose of standardizing column names

    Standardizing column names helps avoid mistakes caused by inconsistent naming like spaces, uppercase letters, or special characters.
  2. Step 2: Recognize the benefits in coding

    Consistent column names make it easier to write and read code, reducing bugs and confusion.
  3. Final Answer:

    To make data easier to work with and reduce errors in code -> Option C
  4. Quick Check:

    Standardizing column names = easier coding [OK]
Hint: Standardize names to avoid typos and errors [OK]
Common Mistakes:
  • Thinking it changes data values
  • Believing it affects DataFrame size
  • Confusing with data type conversion
2. Which of the following is the correct way to convert all column names in a DataFrame df to lowercase?
easy
A. df.columns = df.columns.str.lower()
B. df.columns = lower(df.columns)
C. df.columns = df.columns.lower()
D. df.columns = df.columns.toLowerCase()

Solution

  1. Step 1: Recall pandas string methods for columns

    Column names are an Index object; to apply string methods, use .str accessor.
  2. Step 2: Identify correct syntax for lowercase conversion

    The correct method is df.columns.str.lower(), which converts all column names to lowercase.
  3. Final Answer:

    df.columns = df.columns.str.lower() -> Option A
  4. Quick Check:

    Use .str.lower() on df.columns [OK]
Hint: Use .str accessor for column name string methods [OK]
Common Mistakes:
  • Calling .lower() directly on df.columns (Index object)
  • Using Python's lower() function instead of pandas method
  • Using JavaScript style toLowerCase()
3. Given the DataFrame df with columns [' Name ', 'AGE', 'eMail'], what will be the result of running:
df.columns = df.columns.str.strip().str.lower()
medium
A. [' name', 'AGE', 'eMail']
B. ['Name', 'AGE', 'eMail']
C. [' name ', 'age', 'email']
D. ['name', 'age', 'email']

Solution

  1. Step 1: Apply .str.strip() to remove spaces

    Leading and trailing spaces in ' Name ' are removed, becoming 'Name'.
  2. Step 2: Apply .str.lower() to convert all to lowercase

    All column names become lowercase: 'Name' -> 'name', 'AGE' -> 'age', 'eMail' -> 'email'.
  3. Final Answer:

    ['name', 'age', 'email'] -> Option D
  4. Quick Check:

    Strip spaces + lowercase = ['name', 'age', 'email'] [OK]
Hint: Chain .str.strip() then .str.lower() for clean names [OK]
Common Mistakes:
  • Not removing spaces before lowering case
  • Only stripping spaces but not lowering case
  • Assuming original names remain unchanged
4. You tried to standardize column names using:
df.columns = df.columns.lower()

but got an error. What is the problem?
medium
A. You need to import a special library for .lower()
B. df.columns is an Index object and needs .str accessor for string methods
C. The DataFrame must be converted to a list first
D. You must use uppercase letters instead

Solution

  1. Step 1: Understand the type of df.columns

    df.columns is a pandas Index, not a plain list or string, so string methods need .str accessor.
  2. Step 2: Identify correct usage for string methods on columns

    Use df.columns.str.lower() to apply lowercase conversion to each column name.
  3. Final Answer:

    df.columns is an Index object and needs .str accessor for string methods -> Option B
  4. Quick Check:

    Use .str.lower() on df.columns [OK]
Hint: Use .str before string methods on df.columns [OK]
Common Mistakes:
  • Calling .lower() directly on df.columns
  • Trying to convert df.columns to list unnecessarily
  • Thinking uppercase is required
5. You have a DataFrame df with columns: [' First Name ', 'Last-Name', 'E-mail Address']. You want to standardize these names by:
1. Removing spaces at start and end
2. Replacing dashes and spaces with underscores
3. Converting all letters to lowercase

Which code correctly does this?
hard
A. df.columns = df.columns.str.strip().str.replace('[- ]', '_', regex=True).str.lower()
B. df.columns = df.columns.str.lower().str.strip().str.replace(' ', '_')
C. df.columns = df.columns.replace(' ', '_').lower().strip()
D. df.columns = df.columns.str.strip().str.replace('-', '_').str.lower()

Solution

  1. Step 1: Remove spaces at start and end with .str.strip()

    This cleans ' First Name ' to 'First Name'.
  2. Step 2: Replace dashes and spaces with underscores using regex

    Use .str.replace('[- ]', '_', regex=True) to replace both '-' and ' ' with '_'.
  3. Step 3: Convert all names to lowercase with .str.lower()

    Finally, convert all letters to lowercase for consistency.
  4. Final Answer:

    df.columns = df.columns.str.strip().str.replace('[- ]', '_', regex=True).str.lower() -> Option A
  5. Quick Check:

    Strip, replace with regex, then lower [OK]
Hint: Chain strip, regex replace, then lower for full cleanup [OK]
Common Mistakes:
  • Not using regex to replace both dash and space
  • Wrong order of operations causing partial cleanup
  • Using .replace without .str accessor
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