📖 Scenario: You work at a small online store. The store keeps its product data in JSON format. You want to load this data into Python to analyze it easily.
🎯 Goal: Load a JSON string into a pandas DataFrame, then filter products by price.
📋 What You'll Learn
Create a JSON string variable with product data
Create a price threshold variable
Use pandas read_json to load the JSON string into a DataFrame
Filter the DataFrame to keep only products with price above the threshold
Print the filtered DataFrame
💡 Why This Matters
🌍 Real World
Many companies store data in JSON format. Loading JSON into DataFrames helps analyze and visualize data easily.
💼 Career
Data analysts and scientists often read JSON data from APIs or files to prepare data for analysis.
Progress0 / 4 steps
1
Create JSON string with product data
Create a variable called json_data that holds this exact JSON string: [{"product": "Pen", "price": 1.5}, {"product": "Notebook", "price": 3.0}, {"product": "Eraser", "price": 0.5}]
Data Analysis Python
Hint
Use single quotes outside and double quotes inside for the JSON string.
2
Set price threshold
Create a variable called price_threshold and set it to 1.0
Data Analysis Python
Hint
Just assign 1.0 to price_threshold.
3
Load JSON and filter products
Import pandas as pd. Use pd.read_json to load json_data into a DataFrame called df. Then create a new DataFrame called filtered_df that contains only rows where the price column is greater than price_threshold.
Data Analysis Python
Hint
Use square brackets to filter the DataFrame by condition.
4
Print filtered DataFrame
Print the filtered_df DataFrame to show products with price above the threshold.
Data Analysis Python
Hint
Use print(filtered_df) to display the filtered products.
Practice
(1/5)
1. What does the pandas.read_json() function do in data analysis?
easy
A. It saves a DataFrame to a CSV file.
B. It converts a DataFrame into a JSON string.
C. It loads JSON data into a DataFrame for easy analysis.
D. It visualizes JSON data as a chart.
Solution
Step 1: Understand the purpose of read_json
The function pandas.read_json() is designed to read JSON formatted data and convert it into a DataFrame.
Step 2: Compare options with function behavior
Only It loads JSON data into a DataFrame for easy analysis. correctly describes this behavior. Options B, C, and D describe other unrelated functions.
Final Answer:
It loads JSON data into a DataFrame for easy analysis. -> Option C
Quick Check:
read_json loads JSON to DataFrame [OK]
Hint: Remember: read_json reads JSON into DataFrame [OK]
Common Mistakes:
Confusing read_json with to_json (writing JSON)
Thinking it saves data instead of loading
Assuming it creates visualizations
2. Which of the following is the correct syntax to read a JSON file named data.json into a DataFrame using pandas?
easy
A. df = pd.read_json(data.json)
B. df = pd.read_json('data.json')
C. df = pd.read_json("data.json", header=None)
D. df = pd.read_json[data.json]
Solution
Step 1: Check correct function call syntax
The function pd.read_json() requires the filename as a string argument inside parentheses.
Step 2: Identify correct string usage
df = pd.read_json('data.json') uses single quotes correctly. df = pd.read_json(data.json) misses quotes, causing a NameError. df = pd.read_json("data.json", header=None) uses an invalid header parameter (not supported by read_json). df = pd.read_json[data.json] uses square brackets which is invalid syntax.
Final Answer:
df = pd.read_json('data.json') -> Option B
Quick Check:
Use parentheses and quotes for filename [OK]
Hint: Use parentheses and quotes for file name in read_json [OK]
Common Mistakes:
Forgetting quotes around filename
Using square brackets instead of parentheses
Passing variable without defining it
Using invalid parameters like header= (confusing with read_csv)
3. Given the JSON string '{"name": ["Alice", "Bob"], "age": [25, 30]}', what will be the output of pd.read_json(json_str)?
medium
A. A DataFrame with columns 'name' and 'age' and two rows for Alice and Bob.
B. A DataFrame with two rows and columns named 0 and 1.
C. A Series object with keys 'name' and 'age'.
D. An error because JSON string is invalid.
Solution
Step 1: Understand JSON structure
The JSON string has keys 'name' and 'age' with lists as values, representing columns with two entries each.
Step 2: Behavior of pd.read_json on this JSON
By default, pd.read_json() interprets this as columns with lists of values, creating a DataFrame with columns 'name' and 'age' and two rows.
Final Answer:
A DataFrame with columns 'name' and 'age' and two rows for Alice and Bob. -> Option A
Quick Check:
JSON keys become columns, lists become rows [OK]
Hint: Keys become columns, lists become rows in DataFrame [OK]