📖 Scenario: You work in a small store and have a list of daily sales data. Each entry has the product name, quantity sold, and price per item.
🎯 Goal: You will create a pandas DataFrame from this list of lists to organize the sales data in a table format.
📋 What You'll Learn
Use the pandas library
Create a DataFrame from a list of lists
Assign column names to the DataFrame
Print the DataFrame to see the organized data
💡 Why This Matters
🌍 Real World
Stores and businesses often collect sales data in lists or spreadsheets. Organizing this data into tables helps analyze sales and make decisions.
💼 Career
Data scientists and analysts use pandas DataFrames to clean, organize, and analyze data from many sources.
Progress0 / 4 steps
1
Create the sales data list
Create a variable called sales_data that is a list of lists with these exact entries: ["Apple", 10, 0.5], ["Banana", 5, 0.3], and ["Orange", 8, 0.7].
Pandas
Hint
Use square brackets to create a list of lists. Each inner list has three elements: product name, quantity, and price.
2
Import pandas and set column names
Import the pandas library as pd. Then create a list called columns with these exact strings: "Product", "Quantity", and "Price".
Pandas
Hint
Use import pandas as pd to import pandas. Create a list with the column names exactly as shown.
3
Create the DataFrame from the list
Create a pandas DataFrame called df from the sales_data list using the columns list for column names.
Pandas
Hint
Use pd.DataFrame() with the data and columns arguments.
4
Print the DataFrame
Print the DataFrame df to display the organized sales data.
Pandas
Hint
Use print(df) to show the DataFrame.
Practice
(1/5)
1. What does creating a DataFrame from a list of lists in pandas mainly help you do?
easy
A. Organize data into rows and columns for easy analysis
B. Run SQL queries directly on the list
C. Convert data into images automatically
D. Encrypt data for security
Solution
Step 1: Understand DataFrame purpose
A DataFrame is a table-like structure with rows and columns to organize data.
Step 2: Connect list of lists to DataFrame
A list of lists can be converted into this table format for easier data handling.
Final Answer:
Organize data into rows and columns for easy analysis -> Option A
Quick Check:
DataFrame = Table format [OK]
Hint: DataFrames = tables; list of lists = rows [OK]
Common Mistakes:
Thinking DataFrame encrypts data
Confusing DataFrame with image conversion
Assuming list of lists runs SQL
2. Which of the following is the correct way to create a DataFrame from a list of lists data = [[1, 2], [3, 4]] with columns named 'A' and 'B'?
easy
A. pd.DataFrame(data, columns=['A', 'B'])
B. pd.DataFrame(data, names=['A', 'B'])
C. pd.DataFrame(data, cols=['A', 'B'])
D. pd.DataFrame(data, header=['A', 'B'])
Solution
Step 1: Recall DataFrame column naming syntax
Use the parameter columns to name columns when creating a DataFrame.
Step 2: Check each option
Only pd.DataFrame(data, columns=['A', 'B']) uses columns=['A', 'B'], which is correct syntax.
Final Answer:
pd.DataFrame(data, columns=['A', 'B']) -> Option A
Quick Check:
Use columns= for column names [OK]
Hint: Use columns= to name DataFrame columns [OK]
Common Mistakes:
Using names= instead of columns=
Using cols= or header= which are invalid here
Forgetting to pass columns parameter
3. What will be the output of the following code?
import pandas as pd
data = [[10, 20], [30, 40]]
df = pd.DataFrame(data, columns=['X', 'Y'])
print(df)
medium
A. X Y
0 20 10
1 40 30
B. X Y
0 10 20
1 30 40
C. 0 1
X 10 20
Y 30 40
D. Error: columns parameter missing
Solution
Step 1: Create DataFrame with given data and columns
The list of lists becomes rows; columns=['X', 'Y'] names columns accordingly.
Step 2: Print DataFrame
Output shows index 0 and 1 with values 10,20 and 30,40 under columns X and Y.
Final Answer:
X Y
0 10 20
1 30 40 -> Option B
Quick Check:
DataFrame prints rows with columns X, Y [OK]
Hint: Rows = inner lists; columns = names list [OK]
Common Mistakes:
Swapping row and column values
Confusing index with columns
Expecting error without columns parameter
4. Identify the error in this code snippet:
import pandas as pd
data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=['A'])
print(df)
medium
A. DataFrame cannot be created from list of lists
B. Data should be a dictionary, not list of lists
C. Missing import statement for pandas
D. The columns list length does not match data inner list length
Solution
Step 1: Check data shape and columns length
Each inner list has 2 elements, but columns list has only 1 name.
Step 2: Understand pandas error
pandas requires columns length to match number of elements per row.
Final Answer:
The columns list length does not match data inner list length -> Option D
Quick Check:
Columns count = data columns count [OK]
Hint: Columns count must match inner list length [OK]
Common Mistakes:
Ignoring mismatch between columns and data
Thinking list of lists is invalid input
Forgetting to import pandas
5. You have sales data as a list of lists: sales = [[100, 200], [150, 250], [300, 400]] representing sales in two regions. How do you create a DataFrame with columns 'Region1' and 'Region2' and add a new column 'Total' that sums both regions for each row?