A DataFrame is a table-like data structure in pandas that stores data in rows and columns, similar to a spreadsheet or SQL table.
Click to reveal answer
beginner
How do you create a DataFrame from a list of dictionaries in pandas?
Use the pandas function pd.DataFrame() and pass the list of dictionaries as the argument. Each dictionary becomes a row, and keys become column names.
Click to reveal answer
intermediate
What happens if dictionaries in the list have different keys when creating a DataFrame?
pandas fills missing values with NaN for keys that are not present in some dictionaries, so the DataFrame columns include all keys from all dictionaries.
Click to reveal answer
beginner
Example: Create a DataFrame from this list of dictionaries: [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
Code:<br><code>import pandas as pd
people = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}]
df = pd.DataFrame(people)
print(df)</code><br>Output:<br><code> name age
0 Alice 25
1 Bob 30</code>
Click to reveal answer
beginner
Why is creating a DataFrame from a list of dictionaries useful in real life?
It lets you easily convert structured data from sources like JSON files, APIs, or user input into a table format for analysis and visualization.
Click to reveal answer
What does each dictionary in a list represent when creating a DataFrame from it?
AA row in the DataFrame
BA column in the DataFrame
CA cell in the DataFrame
DA DataFrame index
✗ Incorrect
Each dictionary corresponds to one row, with keys as column names.
If some dictionaries lack certain keys, what does pandas put in those DataFrame cells?
AZero
BNaN (missing value)
CRaises an error
DEmpty string
✗ Incorrect
pandas uses NaN to represent missing values in the DataFrame.
Which pandas function creates a DataFrame from a list of dictionaries?
Apd.Series()
Bpd.read_csv()
Cpd.DataFrame()
Dpd.concat()
✗ Incorrect
pd.DataFrame() is used to create DataFrames from various data structures including lists of dictionaries.
What type of data structure is a DataFrame similar to?
ATable or spreadsheet
BList
CDictionary
DSet
✗ Incorrect
A DataFrame is like a table with rows and columns.
Which of these is a valid way to create a DataFrame from a list of dictionaries named 'data'?
Apd.read_json(data)
Bpd.Series(data)
Cpd.DataFrame.from_dict(data)
Dpd.DataFrame(data)
✗ Incorrect
pd.DataFrame(data) directly converts a list of dictionaries into a DataFrame.
Explain how to create a pandas DataFrame from a list of dictionaries and what happens if some dictionaries have missing keys.
Think about rows, columns, and missing data handling.
You got /4 concepts.
Describe a real-life situation where creating a DataFrame from a list of dictionaries would be helpful.
Consider where you get data in dictionary form.
You got /4 concepts.
Practice
(1/5)
1. What happens when you create a pandas DataFrame from a list of dictionaries?
easy
A. Each dictionary becomes a row, and keys become column names.
B. Each dictionary becomes a column, and keys become row labels.
C. The list is converted into a single column with dictionary objects.
D. The DataFrame will be empty because dictionaries cannot be converted.
Solution
Step 1: Understand DataFrame creation from list of dicts
Each dictionary in the list is treated as one row in the DataFrame.
Step 2: Identify how keys are used
Keys from dictionaries become the column names in the DataFrame.
Final Answer:
Each dictionary becomes a row, and keys become column names. -> Option A
Quick Check:
List of dicts -> rows, keys -> columns [OK]
Hint: Remember: dict keys become columns, dicts become rows [OK]
Common Mistakes:
Thinking dictionaries become columns instead of rows
Assuming keys become row labels
Believing the DataFrame will be empty
2. Which of the following is the correct syntax to create a DataFrame from a list of dictionaries named data?
easy
A. df = pd.DataFrame(data)
B. df = pd.DataFrame.from_list(data)
C. df = pd.DataFrame.create(data)
D. df = pd.DataFrame.list(data)
Solution
Step 1: Recall pandas DataFrame constructor
The correct way to create a DataFrame from data is using pd.DataFrame().
Step 2: Check method names
There is no from_list, create, or list method in pandas DataFrame.
Final Answer:
df = pd.DataFrame(data) -> Option A
Quick Check:
Use pd.DataFrame() constructor [OK]
Hint: Use pd.DataFrame(your_list) to create DataFrame [OK]
Common Mistakes:
Using non-existent methods like from_list or create
Confusing DataFrame constructor syntax
Missing pd prefix or parentheses
3. Given the code:
import pandas as pd
data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'city': 'NY'}]
df = pd.DataFrame(data)
print(df)
What will be the output?
medium
A. A DataFrame with columns 'name' and 'city' only.
B. A DataFrame with only columns 'name' and 'age', ignoring 'city'.
C. An error because dictionaries have different keys.
D. A DataFrame with columns 'name', 'age', 'city' and NaN where data is missing.
Solution
Step 1: Check keys across dictionaries
Keys are 'name', 'age' in first dict and 'name', 'city' in second dict.
Step 2: Understand pandas behavior with missing keys
Pandas creates columns for all keys found and fills missing values with NaN.
Final Answer:
DataFrame with columns 'name', 'age', 'city' and NaN where data is missing. -> Option D
Quick Check:
Missing keys -> NaN in DataFrame [OK]
Hint: Missing keys become NaN in DataFrame columns [OK]