📖 Scenario: Imagine you work in a small store. You have a list of products, their prices, and how many you sold each day. You want to organize this information so you can easily see and analyze it.
🎯 Goal: You will create a simple table using a DataFrame to hold product sales data. Then, you will add a rule to find products with sales above a certain number. Finally, you will list those products and their sales.
📋 What You'll Learn
Create a DataFrame with product names, prices, and sales
Create a variable for the sales threshold
Use a filter to find products with sales above the threshold
Print the filtered DataFrame
💡 Why This Matters
🌍 Real World
Stores and businesses use tables like DataFrames to keep track of products, prices, and sales to make smart decisions.
💼 Career
Data scientists and analysts use DataFrames daily to clean, explore, and analyze data quickly and clearly.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales_data with these exact columns and values: Product with ['Apple', 'Banana', 'Carrot'], Price with [0.5, 0.3, 0.2], and Sales with [30, 45, 25].
Data Analysis Python
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.
2
Set the sales threshold
Create a variable called sales_threshold and set it to 30.
Data Analysis Python
Hint
Just create a variable and assign the number 30 to it.
3
Filter products with sales above the threshold
Create a new DataFrame called top_sellers by filtering sales_data to include only rows where the Sales column is greater than sales_threshold.
Data Analysis Python
Hint
Use sales_data[condition] where condition checks if Sales is greater than sales_threshold.
4
Print the filtered top sellers
Print the top_sellers DataFrame to show products with sales above the threshold.
Data Analysis Python
Hint
Use print(top_sellers) to show the filtered table.
Practice
(1/5)
1. Why is a DataFrame considered the core data structure in pandas for data analysis?
easy
A. Because it is used to write data to files only.
B. Because it only stores data as a list of numbers without labels.
C. Because it is a type of graph used for visualization.
D. Because it organizes data in rows and columns like a table, making it easy to understand and use.
Solution
Step 1: Understand DataFrame structure
A DataFrame organizes data in rows and columns, similar to a spreadsheet or table.
Step 2: Recognize its role in data analysis
This tabular structure makes it easy to view, manipulate, and analyze data efficiently.
Final Answer:
Because it organizes data in rows and columns like a table, making it easy to understand and use. -> Option D
Quick Check:
DataFrame = tabular data structure [OK]
Hint: Think of DataFrame as a spreadsheet table for data [OK]
Common Mistakes:
Confusing DataFrame with simple lists or arrays
Thinking DataFrame is only for visualization
Believing DataFrame stores data without labels
2. Which of the following is the correct way to create a simple DataFrame in pandas?
easy
A. pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
B. pd.DataFrame['Name', 'Age'] = ['Alice', 25]
C. pd.DataFrame(Name = 'Alice', Age = 25)
D. pd.DataFrame(['Alice', 25], ['Bob', 30])
Solution
Step 1: Check pandas DataFrame creation syntax
The correct way is to pass a dictionary where keys are column names and values are lists of column data.
Step 2: Verify each option
pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]}) uses a dictionary with lists, which is correct. Others have syntax errors or wrong formats.
Final Answer:
pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]}) -> Option A
Quick Check:
DataFrame creation = dictionary of lists [OK]
Hint: Use dictionary with column names as keys to create DataFrame [OK]
Common Mistakes:
Using square brackets instead of parentheses for DataFrame
Passing single values instead of lists for columns
Incorrect assignment syntax
3. What will be the output of the following code?
import pandas as pd
data = {'City': ['NY', 'LA', 'SF'], 'Population': [8, 4, 3]}
df = pd.DataFrame(data)
print(df.loc[1, 'City'])
medium
A. NY
B. LA
C. SF
D. KeyError
Solution
Step 1: Understand DataFrame creation
The DataFrame has rows indexed 0, 1, 2 with 'City' values 'NY', 'LA', 'SF' respectively.
Step 2: Access value at row 1, column 'City'
Using df.loc[1, 'City'] accesses the second row's 'City' value, which is 'LA'.
Final Answer:
LA -> Option B
Quick Check:
Row 1 City = LA [OK]
Hint: loc[row, column] accesses exact cell by label [OK]
Common Mistakes:
Confusing loc with iloc (integer position)
Assuming index starts at 1 instead of 0
Expecting KeyError due to wrong indexing
4. Identify the error in this code snippet that tries to create a DataFrame:
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': 25}
df = pd.DataFrame(data)
print(df)
medium
A. The print statement syntax is incorrect.
B. DataFrame cannot be created without specifying columns explicitly.
C. Data dictionary values must be lists or arrays, not single values.
D. Pandas library is not imported correctly.
Solution
Step 1: Check data dictionary format
The dictionary has a list for 'Name' and a single value for 'Age' instead of matching lists, which is invalid for DataFrame creation.
Step 2: Understand DataFrame input requirements
DataFrame expects each column to have a list or array of values, even if only one row.
Final Answer:
Data dictionary values must be lists or arrays, not single values. -> Option C
Quick Check:
DataFrame columns need list values [OK]
Hint: Always use lists for column data when creating DataFrame [OK]
Common Mistakes:
Passing single values instead of lists in data dictionary
Assuming DataFrame can infer single values as rows
Ignoring error messages about data format
5. You have a list of dictionaries representing sales data: