Challenge - 5 Problems
Pandas Columns and Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of accessing the columns attribute?
Given the following pandas DataFrame:
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob'],
'Age': [25, 30]
})
What is the output of df.columns?Pandas
import pandas as pd df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]}) print(df.columns)
Attempts:
2 left
💡 Hint
The columns attribute returns the labels of the columns, not the data itself.
✗ Incorrect
The
columns attribute returns an Index object containing the column labels of the DataFrame.❓ query_result
intermediate2:00remaining
What does the index attribute represent?
Consider this DataFrame:
import pandas as pd
df = pd.DataFrame({
'City': ['Paris', 'London'],
'Population': [2148000, 8982000]
})
What is the output of df.index?Pandas
import pandas as pd df = pd.DataFrame({'City': ['Paris', 'London'], 'Population': [2148000, 8982000]}) print(df.index)
Attempts:
2 left
💡 Hint
The index attribute shows the row labels, not the column names or data.
✗ Incorrect
By default, pandas assigns a RangeIndex starting at 0 for rows if no index is specified.
📝 Syntax
advanced2:00remaining
Which option correctly sets a new index for a DataFrame?
Given a DataFrame
df with columns 'A' and 'B', which code correctly sets column 'A' as the index?Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
Attempts:
2 left
💡 Hint
To change the DataFrame index permanently, you need to use the inplace parameter.
✗ Incorrect
Option B correctly sets column 'A' as the index and modifies df in place. Option B returns a new DataFrame but does not change df unless assigned. Option B assigns the index but does not remove column 'A'. Option B is invalid syntax.
❓ query_result
advanced2:00remaining
What is the output of resetting the index?
Given this DataFrame with a custom index:
import pandas as pd
df = pd.DataFrame({'X': [10, 20]}, index=['a', 'b'])
reset_df = df.reset_index()
print(reset_df)Pandas
import pandas as pd df = pd.DataFrame({'X': [10, 20]}, index=['a', 'b']) reset_df = df.reset_index() print(reset_df)
Attempts:
2 left
💡 Hint
Resetting index moves the index into a column named 'index' by default.
✗ Incorrect
reset_index() moves the current index into a new column named 'index' and creates a new default integer index.
🧠 Conceptual
expert2:00remaining
Why is it important to understand the difference between columns and index in pandas?
Which of the following best explains why knowing the difference between
columns and index attributes is crucial when working with pandas DataFrames?Attempts:
2 left
💡 Hint
Think about how pandas uses index and columns to organize data.
✗ Incorrect
Columns contain the data fields, while index labels the rows. This distinction affects how you select, join, and align data in pandas.