Bird
Raised Fist0
Pandasdata~10 mins

Standardizing column names in Pandas - Interactive Code Practice

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

Complete the code to convert all column names in the DataFrame to lowercase.

Pandas
df.columns = df.columns.[1]()
Drag options to blanks, or click blank then click option'
Atitle
Blower
Cupper
Dcapitalize
Attempts:
3 left
💡 Hint
Common Mistakes
Using upper() instead of lower() changes columns to uppercase.
Using title() or capitalize() changes only first letters, not all lowercase.
2fill in blank
medium

Complete the code to replace spaces in column names with underscores.

Pandas
df.columns = df.columns.str.[1](' ', '_')
Drag options to blanks, or click blank then click option'
Astrip
Bsplit
Creplace
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using strip() removes spaces only at start or end, not inside.
Using split() breaks strings into lists, not replacing characters.
3fill in blank
hard

Fix the error in the code to remove leading and trailing spaces from column names.

Pandas
df.columns = df.columns.str.[1]()
Drag options to blanks, or click blank then click option'
Astrip
Btrim
Cclean
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using trim() which is not a pandas string method.
Using clean() or remove() which do not exist for strings.
4fill in blank
hard

Fill both blanks to create a dictionary that maps old column names to lowercase names without spaces.

Pandas
new_names = {col: col.[1]().[2](' ', '_') for col in df.columns}
Drag options to blanks, or click blank then click option'
Alower
Breplace
Cstrip
Dupper
Attempts:
3 left
💡 Hint
Common Mistakes
Using upper() instead of lower() changes case wrongly.
Using strip() instead of replace() does not change spaces inside.
5fill in blank
hard

Fill all three blanks to rename columns by stripping spaces, converting to lowercase, and replacing spaces with underscores.

Pandas
df.columns = df.columns.str.[1]().str.[2]().str.[3](' ', '_')
Drag options to blanks, or click blank then click option'
Astrip
Blower
Creplace
Dupper
Attempts:
3 left
💡 Hint
Common Mistakes
Changing order of methods can cause errors.
Using upper() instead of lower() changes case wrongly.

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