0
0
Pandasdata~20 mins

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

Choose your learning style9 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.