0
0
Pandasdata~20 mins

columns and index attributes in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pandas Columns and Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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)
ARangeIndex(start=0, stop=2, step=1)
B['Name', 'Age']
C['Alice', 'Bob']
DIndex(['Name', 'Age'], dtype='object')
Attempts:
2 left
💡 Hint
The columns attribute returns the labels of the columns, not the data itself.
query_result
intermediate
2: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)
ARangeIndex(start=0, stop=2, step=1)
BIndex(['City', 'Population'], dtype='object')
C['Paris', 'London']
D['City', 'Population']
Attempts:
2 left
💡 Hint
The index attribute shows the row labels, not the column names or data.
📝 Syntax
advanced
2: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]})
Adf.set_index('A')
Bdf.set_index('A', inplace=True)
Cdf.index('A')
Ddf.index = df['A']
Attempts:
2 left
💡 Hint
To change the DataFrame index permanently, you need to use the inplace parameter.
query_result
advanced
2: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)
A
  level_0   X
0       a  10
1       b  20
B
     X
0  10
1  20
C
  index   X
0     a  10
1     b  20
D
  index  X
a    10
b    20
Attempts:
2 left
💡 Hint
Resetting index moves the index into a column named 'index' by default.
🧠 Conceptual
expert
2: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?
ABecause columns hold the data values and index defines the row labels, affecting data selection and alignment.
BBecause columns represent row labels and index represents column labels, mixing them causes errors.
CBecause both columns and index store data values, but index is faster to access.
DBecause index is only used for sorting, while columns are used for filtering data.
Attempts:
2 left
💡 Hint
Think about how pandas uses index and columns to organize data.