0
0
Pandasdata~15 mins

columns and index attributes in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use sales_data.index to get the index labels.