Bird
Raised Fist0
Pandasdata~20 mins

Specifying column names and index in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Column and Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
1:30remaining
What is the output of this code when specifying column names?

Given the following code, what will be the column names of the resulting DataFrame?

import pandas as pd

data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=['A', 'B'])
print(df.columns.tolist())
Pandas
import pandas as pd

data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=['A', 'B'])
print(df.columns.tolist())
A['1', '2']
B['0', '1']
C['A', 'B']
D['a', 'b']
Attempts:
2 left
💡 Hint

Look at the columns parameter in the DataFrame constructor.

❓ query_result
intermediate
1:30remaining
What is the index of the DataFrame after specifying it explicitly?

Consider this code snippet. What will be the index of the DataFrame?

import pandas as pd

data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data, index=['x', 'y'])
print(df.index.tolist())
Pandas
import pandas as pd

data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data, index=['x', 'y'])
print(df.index.tolist())
A['x', 'y']
B[0, 1]
C['Name', 'Age']
D['Alice', 'Bob']
Attempts:
2 left
💡 Hint

Check the index parameter in the DataFrame constructor.

📝 Syntax
advanced
2:00remaining
Which option causes an error when specifying columns and index?

Which of the following DataFrame constructions will cause an error?

import pandas as pd

data = [[1, 2], [3, 4]]
Apd.DataFrame(data, columns=['A', 'B'], index=['row1', 'row2'])
Bpd.DataFrame(data, columns='A,B', index=['row1', 'row2'])
C)]'2wor' ,'1wor'[=xedni ,]'B' ,'A'[=snmuloc ,atad(emarFataD.dp
Dd.DataFrame(data, columns=['A', 'B'], index=['row1', 'row2'])
Attempts:
2 left
💡 Hint

Check the type expected for the columns parameter.

❓ query_result
advanced
2:00remaining
What is the output of this code specifying columns and index with mismatched lengths?

What happens when the number of columns and data columns do not match?

import pandas as pd

data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=['A', 'B', 'C'])
print(df)
Pandas
import pandas as pd

data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=['A', 'B', 'C'])
print(df)
ARaises a ValueError about columns length mismatch
BCreates DataFrame with NaN in column 'C'
CIgnores extra column name 'C' and creates DataFrame with columns 'A' and 'B'
DCreates DataFrame with only one column 'A'
Attempts:
2 left
💡 Hint

Check if pandas allows columns list length to differ from data width.

🧠 Conceptual
expert
2:30remaining
Why specify both columns and index explicitly in a DataFrame?

Which of the following is the best reason to specify both columns and index explicitly when creating a DataFrame?

ATo convert all data to strings
BTo reduce the memory usage of the DataFrame
CTo automatically sort the data by the index and columns
DTo ensure the DataFrame has meaningful row and column labels for easier data access and readability
Attempts:
2 left
💡 Hint

Think about how labels help when working with data.

Practice

(1/5)
1. What is the purpose of specifying column names when creating a pandas DataFrame?
easy
A. To change the data type of columns automatically
B. To delete unwanted rows from the DataFrame
C. To sort the rows in ascending order
D. To label each data column clearly for easier understanding

Solution

  1. Step 1: Understand column names role

    Column names act like labels on folders, helping identify what data each column holds.
  2. Step 2: Recognize their benefit

    Clear labels make it easier to read and work with data without confusion.
  3. Final Answer:

    To label each data column clearly for easier understanding -> Option D
  4. Quick Check:

    Column names label data columns [OK]
Hint: Column names are like labels on folders for clarity [OK]
Common Mistakes:
  • Thinking column names change data types
  • Confusing column names with sorting
  • Believing column names delete rows
2. Which of the following is the correct way to specify column names when creating a DataFrame from a list of lists?
easy
A. pd.DataFrame(data, columns=['Name', 'Age'])
B. pd.DataFrame(data, index=['Name', 'Age'])
C. pd.DataFrame(data, labels=['Name', 'Age'])
D. pd.DataFrame(data, names=['Name', 'Age'])

Solution

  1. Step 1: Recall DataFrame syntax

    To name columns, use the 'columns' parameter with a list of names.
  2. Step 2: Identify correct option

    Only pd.DataFrame(data, columns=['Name', 'Age']) uses 'columns' correctly; others use invalid parameters.
  3. Final Answer:

    pd.DataFrame(data, columns=['Name', 'Age']) -> Option A
  4. Quick Check:

    Use 'columns' to name columns [OK]
Hint: Use 'columns' parameter to name columns in DataFrame [OK]
Common Mistakes:
  • Using 'index' to name columns instead of rows
  • Using non-existent parameters like 'labels' or 'names'
  • Confusing column names with index labels
3. What will be the output of this code?
import pandas as pd
data = [[10, 20], [30, 40]]
df = pd.DataFrame(data, columns=['A', 'B'], index=['row1', 'row2'])
print(df)
medium
A. A table with default numeric columns and rows indexed as 'row1' and 'row2'
B. A table with columns named 'A' and 'B' and rows indexed as 'row1' and 'row2'
C. A table with columns named 'A' and 'B' and default numeric row indices
D. SyntaxError due to wrong parameters

Solution

  1. Step 1: Analyze DataFrame creation

    Columns are set to ['A', 'B'] and index to ['row1', 'row2'], so labels replace defaults.
  2. Step 2: Predict printed output

    Printed DataFrame shows data with specified column names and row indices.
  3. Final Answer:

    A table with columns named 'A' and 'B' and rows indexed as 'row1' and 'row2' -> Option B
  4. Quick Check:

    Columns and index labels set correctly [OK]
Hint: Columns and index parameters label columns and rows respectively [OK]
Common Mistakes:
  • Assuming default numeric indices when index is specified
  • Confusing columns and index parameters
  • Expecting syntax error with correct parameters
4. Identify the error in this code snippet:
import pandas as pd
data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns='X,Y', index=['r1', 'r2'])
print(df)
medium
A. Data should be a dictionary, not a list
B. The index list length does not match data rows
C. The columns parameter should be a list, not a string
D. print(df) is missing parentheses

Solution

  1. Step 1: Check columns parameter type

    Columns must be a list of strings, but 'X,Y' is a single string.
  2. Step 2: Understand impact

    Passing a string causes pandas to treat each character as a column name, leading to unexpected columns.
  3. Final Answer:

    The columns parameter should be a list, not a string -> Option C
  4. Quick Check:

    Columns must be list, not string [OK]
Hint: Always use list for columns, not a comma string [OK]
Common Mistakes:
  • Passing columns as a comma-separated string
  • Ignoring mismatch between index length and data rows
  • Thinking print needs no parentheses in Python 3
5. You have this nested list:
data = [[1, 2], [3, 4], [5, 6]]

You want to create a DataFrame with columns named 'X' and 'Y' and index labels as 'a', 'b', 'c'. Which code correctly does this?
hard
A. pd.DataFrame(data, columns=['X', 'Y'], index=['a', 'b', 'c'])
B. pd.DataFrame(data, columns=['a', 'b', 'c'], index=['X', 'Y'])
C. pd.DataFrame(data, index=['X', 'Y'], columns=['a', 'b', 'c'])
D. pd.DataFrame(data, labels=['X', 'Y'], rows=['a', 'b', 'c'])

Solution

  1. Step 1: Match columns and index to data shape

    Data has 3 rows and 2 columns, so columns list must have 2 names and index list 3 labels.
  2. Step 2: Identify correct parameter usage

    pd.DataFrame(data, columns=['X', 'Y'], index=['a', 'b', 'c']) correctly uses columns=['X', 'Y'] and index=['a', 'b', 'c'] matching data shape.
  3. Final Answer:

    pd.DataFrame(data, columns=['X', 'Y'], index=['a', 'b', 'c']) -> Option A
  4. Quick Check:

    Columns and index match data shape [OK]
Hint: Columns = data columns, index = data rows labels [OK]
Common Mistakes:
  • Swapping columns and index lists
  • Using wrong parameter names like 'labels' or 'rows'
  • Mismatching list lengths with data shape
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