What does setting a column as an index in pandas do?
It makes that column the row labels, which helps to identify rows uniquely and access data more easily.
Click to reveal answer
beginner
How do you set a column named 'ID' as the index in a pandas DataFrame called df?
Use df.set_index('ID', inplace=True) to set the 'ID' column as the index and change df directly.
Click to reveal answer
intermediate
What happens if you set a column as index without inplace=True?
The method returns a new DataFrame with the column set as index, but the original DataFrame stays unchanged.
Click to reveal answer
intermediate
Can you set multiple columns as a multi-level index in pandas?
Yes, by passing a list of column names to set_index(), like df.set_index(['col1', 'col2']).
Click to reveal answer
beginner
Why might you want to reset the index after setting a column as index?
To turn the index back into a regular column, making it easier to manipulate or export the data.
Click to reveal answer
Which pandas method sets a column as the index of a DataFrame?
Aset_index()
Bset_column()
Cmake_index()
Dindex_column()
✗ Incorrect
The correct method to set a column as index is set_index().
What does inplace=True do when used with set_index()?
ADeletes the DataFrame
BReturns a new DataFrame without changing the original
CModifies the original DataFrame directly
DCreates a copy of the index
✗ Incorrect
inplace=True changes the original DataFrame instead of returning a new one.
How do you set multiple columns as a multi-level index?
ACall <code>set_index()</code> multiple times
BPass a list of column names to <code>set_index()</code>
CUse <code>multi_index()</code> method
DSet index on one column only
✗ Incorrect
Passing a list like ['col1', 'col2'] to set_index() creates a multi-level index.
What type of object is returned by set_index() if inplace=False?
AA new DataFrame with the index set
BA Series
CNone
DThe original DataFrame unchanged
✗ Incorrect
Without inplace=True, set_index() returns a new DataFrame with the index set.
Why is setting a column as index useful?
AIt sorts the DataFrame automatically
BIt deletes the column permanently
CIt converts the DataFrame to a list
DIt helps identify rows uniquely and speeds up data access
✗ Incorrect
Setting an index helps label rows uniquely and can make data access faster and clearer.
Explain how to set a column as the index in a pandas DataFrame and why you might want to do this.
Think about how row labels help you find data quickly.
You got /3 concepts.
Describe how to create a multi-level index using multiple columns in pandas.
Imagine organizing data by two or more categories at once.
You got /3 concepts.
Practice
(1/5)
1. What does the set_index() method do in pandas?
easy
A. It sorts the DataFrame by a column.
B. It deletes a column from the DataFrame.
C. It adds a new column to the DataFrame.
D. It sets a specified column as the index of the DataFrame.
Solution
Step 1: Understand the purpose of set_index()
The set_index() method changes the row labels to the values of a specified column.
Step 2: Compare with other options
Options A, B, and D describe different DataFrame operations, not setting the index.
Final Answer:
It sets a specified column as the index of the DataFrame. -> Option D
Quick Check:
set_index() changes row labels to a column [OK]
Hint: Remember: set_index() changes row labels to a column [OK]
Common Mistakes:
Confusing set_index() with drop() method
Thinking set_index() adds a new column
Assuming set_index() sorts data
2. Which of the following is the correct syntax to set the column 'Name' as the index of DataFrame df without modifying df itself?
easy
A. df.set_index('Name', inplace=True)
B. df.set_index('Name')
C. df.set_index(Name, inplace=False)
D. df.set_index('Name', drop=False)
Solution
Step 1: Understand inplace parameter
By default, inplace=False, so df.set_index('Name') returns a new DataFrame without changing df.
Step 2: Analyze options
df.set_index('Name', inplace=True) modifies df inplace. df.set_index('Name', inplace=False) is invalid because inplace=False is default and usually omitted. df.set_index('Name', drop=False) keeps the column but does not specify inplace, so it returns a new DataFrame.
Final Answer:
df.set_index('Name') -> Option B
Quick Check:
Default inplace=False means returns new DataFrame [OK]
Hint: Omit inplace to keep original DataFrame unchanged [OK]
Common Mistakes:
Using inplace=True when wanting a new DataFrame
Confusing drop parameter with inplace
Adding invalid parameters
3. Given the DataFrame df below:
ID Score
0 101 88
1 102 92
2 103 85
What will be the output of df.set_index('ID')?
medium
A.
ID Score
0 101 88
1 102 92
2 103 85
B.
ID Score
0 101 88
1 102 92
2 103 85
C.
Score
ID
101 88
102 92
103 85
D.
Score
ID
0 88
1 92
2 85
Solution
Step 1: Understand set_index('ID')
This sets the 'ID' column as the index, so 'ID' values become row labels and are no longer columns.
Step 2: Check output format
The resulting DataFrame shows 'Score' as the only column, indexed by 'ID' values 101, 102, 103.
Final Answer:
Score
ID
101 88
102 92
103 85 -> Option C
Quick Check:
set_index moves column to index =
Score
ID
101 88
102 92
103 85
[OK]
Hint: Index column moves from data to row labels [OK]
Common Mistakes:
Expecting the index column to remain as a data column
Confusing output with original DataFrame
Misreading index as a new column
4. You run the code:
df.set_index('Age', inplace=True)
but get a KeyError: 'Age'. What is the most likely cause?
medium
A. The column 'Age' does not exist in the DataFrame.
B. You forgot to import pandas.
C. You need to reset the index before setting a new one.
D. The inplace parameter must be False.
Solution
Step 1: Understand KeyError meaning
A KeyError means the specified key (column) is not found in the DataFrame.
Step 2: Analyze options
The column 'Age' does not exist in the DataFrame. correctly identifies the missing column. You forgot to import pandas. is unrelated to KeyError. You need to reset the index before setting a new one. is unnecessary; you can set any existing column as index. The inplace parameter must be False. is incorrect; inplace can be True or False.
Final Answer:
The column 'Age' does not exist in the DataFrame. -> Option A
Quick Check:
KeyError means missing column [OK]
Hint: Check column names before set_index to avoid KeyError [OK]
Common Mistakes:
Assuming inplace=True causes KeyError
Ignoring column name typos
Thinking reset_index is required first
5. You have a DataFrame df with columns ['City', 'Population', 'Area']. You want to set 'City' as the index but keep it as a column too. Which code achieves this?
hard
A. df.set_index('City', drop=False, inplace=True)
B. df.set_index('City', inplace=True)
C. df.set_index('City', drop=True, inplace=False)
D. df.set_index('City', keep=True)
Solution
Step 1: Understand drop parameter
By default, drop=True removes the column from data when setting it as index. To keep it, use drop=False.
Step 2: Apply inplace to modify original DataFrame
Using inplace=True changes df directly.
Step 3: Analyze options
df.set_index('City', drop=False, inplace=True) correctly uses drop=False and inplace=True. df.set_index('City', inplace=True) drops the column. df.set_index('City', drop=True, inplace=False) returns new DataFrame but drops column. df.set_index('City', keep=True) uses invalid parameter keep.
Final Answer:
df.set_index('City', drop=False, inplace=True) -> Option A
Quick Check:
drop=False keeps column when setting index [OK]
Hint: Use drop=False to keep column when setting index [OK]
Common Mistakes:
Forgetting drop=False to keep the column
Using invalid parameters like keep=True
Not using inplace when wanting to modify original DataFrame
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.