Exploring columns and index attributes in pandas DataFrame
📖 Scenario: You are working with sales data for a small store. You want to organize the data in a table format using pandas to easily see the products sold, their prices, and quantities.
🎯 Goal: Create a pandas DataFrame with sales data, then explore its columns and index attributes to understand the structure of the data.
📋 What You'll Learn
Create a pandas DataFrame named sales_data with three columns: 'Product', 'Price', and 'Quantity'.
Set the index of the DataFrame to be the product names.
Create a variable cols that stores the column names of sales_data.
Create a variable idx that stores the index labels of sales_data.
💡 Why This Matters
🌍 Real World
Organizing and exploring tabular data like sales records, inventory, or customer lists is common in many jobs.
💼 Career
Knowing how to use pandas DataFrames and their attributes helps in data analysis, reporting, and preparing data for machine learning.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales_data with these exact entries: products 'Apple', 'Banana', 'Cherry'; prices 0.5, 0.3, 0.2; quantities 10, 20, 15.
Pandas
Hint
Use pd.DataFrame and pass a dictionary with keys as column names and values as lists of data.
2
Set the index to product names
Set the index of sales_data to the 'Product' column using the set_index() method and update sales_data with this change.
Pandas
Hint
Use sales_data.set_index('Product') and assign it back to sales_data.
3
Get the columns attribute
Create a variable called cols and assign it the columns attribute of sales_data.
Pandas
Hint
Use sales_data.columns to get the column names.
4
Get the index attribute
Create a variable called idx and assign it the index attribute of sales_data.
Pandas
Hint
Use sales_data.index to get the index labels.
Practice
(1/5)
1. What does the columns attribute in a pandas DataFrame represent?
easy
A. The number of rows in the DataFrame
B. The data types of each column
C. The names of the columns in the DataFrame
D. The values of the first row
Solution
Step 1: Understand the role of columns in pandas
The columns attribute stores the labels for each column in the DataFrame, like the headers in a table.
Step 2: Compare with other attributes
Other attributes like index store row labels, not column names.
Final Answer:
The names of the columns in the DataFrame -> Option C
Quick Check:
columns = column names [OK]
Hint: Columns attribute holds column names, like table headers [OK]
Common Mistakes:
Confusing columns with index
Thinking columns store data types
Assuming columns count rows
2. Which of the following is the correct way to get the row labels of a pandas DataFrame df?
easy
A. df.index
B. df.columns
C. df.labels
D. df.rows
Solution
Step 1: Recall the attribute for row labels
The index attribute in pandas stores the row labels or indices.
Step 2: Verify other options
columns is for column names; labels and rows are not valid pandas attributes.
Final Answer:
df.index -> Option A
Quick Check:
index = row labels [OK]
Hint: Use df.index to get row labels, not df.columns [OK]
D. The index list length does not match the number of rows
Solution
Step 1: Check DataFrame shape
The DataFrame has 2 rows but the new index list has 3 labels.
Step 2: Understand index assignment rules
When assigning to df.index, the length must match the number of rows exactly.
Final Answer:
The index list length does not match the number of rows -> Option D
Quick Check:
Index length must equal row count [OK]
Hint: Index length must match rows count exactly [OK]
Common Mistakes:
Assigning index list with wrong length
Thinking columns rename affects index
Assuming any list can be index regardless of length
5. You have a DataFrame df with columns ['name', 'age', 'city'] and default numeric index. How do you rename the columns to ['Name', 'Age', 'City'] and set the index to the 'name' column in one step?
hard
A. df.columns = ['Name', 'Age', 'City']; df.set_index('name', inplace=True)
B. df.rename(columns={'name':'Name', 'age':'Age', 'city':'City'}, inplace=True); df.set_index('Name', inplace=True)
C. df.columns = ['Name', 'Age', 'City']; df.index = df['Name']
D. df.rename(index={'name':'Name'}, inplace=True); df.columns = ['Name', 'Age', 'City']
Solution
Step 1: Rename columns properly
Use df.rename(columns=..., inplace=True) to rename columns safely without losing data.
Step 2: Set the index to the renamed 'Name' column
Use df.set_index('Name', inplace=True) to set the index to the 'Name' column after renaming.
Final Answer:
df.rename(columns={'name':'Name', 'age':'Age', 'city':'City'}, inplace=True); df.set_index('Name', inplace=True) -> Option B
Quick Check:
Rename columns then set index by column name [OK]
Hint: Rename columns with rename(), then set_index() by new name [OK]
Common Mistakes:
Assigning columns list directly without rename()
Setting index before renaming columns
Assigning index to a Series without set_index()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.