📖 Scenario: You work in a small bookstore. You want to organize your book sales data in a clear table format to analyze it easily.
🎯 Goal: Create a pandas DataFrame to represent book sales with labels for rows and columns. Then display the table.
📋 What You'll Learn
Use pandas to create a DataFrame
Include book titles as row labels
Include sales data as columns
Print the DataFrame to show the table
💡 Why This Matters
🌍 Real World
Organizing sales or inventory data in tables helps businesses track performance clearly.
💼 Career
Data analysts and scientists often use pandas DataFrames to manage and analyze labeled data efficiently.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales_data with these exact entries: 'Title' as a list of 'Book A', 'Book B', 'Book C'; 'Copies Sold' as a list of 150, 200, 120; and 'Revenue' as a list of 3000, 5000, 2400.
Pandas
Hint
Use a dictionary with keys 'Title', 'Copies Sold', and 'Revenue'. Each key should have a list of values.
2
Import pandas and create DataFrame
Import pandas as pd. Then create a DataFrame called df from the sales_data dictionary.
Pandas
Hint
Use import pandas as pd and then pd.DataFrame() with the dictionary.
3
Set the 'Title' column as row labels
Use the set_index method on df to set the 'Title' column as the index (row labels). Assign the result back to df.
Pandas
Hint
Use df.set_index('Title') and assign it back to df.
4
Print the DataFrame
Print the DataFrame df to display the labeled two-dimensional table.
Pandas
Hint
Use print(df) to show the table.
Practice
(1/5)
1.
What is a pandas DataFrame best described as?
easy
A. A one-dimensional list of numbers
B. A type of database server
C. A function to plot graphs
D. A labeled two-dimensional table with rows and columns
Solution
Step 1: Understand the structure of a DataFrame
A DataFrame organizes data in rows and columns with labels for both, like a spreadsheet.
Step 2: Compare options to definition
Only "A labeled two-dimensional table with rows and columns" describes a labeled two-dimensional table, matching the DataFrame concept.
Final Answer:
A labeled two-dimensional table with rows and columns -> Option D
Quick Check:
DataFrame = labeled 2D table [OK]
Hint: Remember: DataFrame = table with row and column labels [OK]
Common Mistakes:
Confusing DataFrame with a list or array
Thinking DataFrame is a plotting tool
Mixing DataFrame with database servers
2.
Which of the following is the correct way to create a pandas DataFrame from a dictionary?
import pandas as pd
# Which line correctly creates a DataFrame?