We use a dictionary to quickly make a table of data. This helps us organize and analyze information easily.
You have data stored as key-value pairs and want to see it as a table.
You want to convert survey answers stored in a dictionary into a spreadsheet format.
You collected data from different sources and combined it into a dictionary, now want to analyze it.
You want to create a small dataset for testing or learning purposes.
Syntax
Pandas
import pandas as pd
df = pd.DataFrame(data)
data is a dictionary where keys become column names and values are lists of column data.
All lists in the dictionary should be the same length to avoid errors.
Examples
This creates a DataFrame with two columns: Name and Age.
Pandas
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
Here, the DataFrame shows cities and their populations.
Pandas
import pandas as pd
data = {'City': ['NY', 'LA', 'Chicago'], 'Population': [8000000, 4000000, 2700000]}
df = pd.DataFrame(data)
print(df)
Sample Program
This program makes a table of students with their scores and if they passed. It prints the table.
Pandas
import pandas as pd
# Create a dictionary with data
student_data = {
'Student': ['John', 'Emma', 'Sophia'],
'Score': [88, 92, 95],
'Passed': [True, True, True]
}
# Convert dictionary to DataFrame
df = pd.DataFrame(student_data)
# Show the DataFrameprint(df)
OutputSuccess
Important Notes
If lists in the dictionary have different lengths, pandas will raise an error.
You can also use an OrderedDict if you want to keep columns in a specific order.
Summary
Use a dictionary with lists to create a DataFrame easily.
Keys become column names, values become column data.
Make sure all lists have the same length for a valid table.
Practice
(1/5)
1. What happens when you create a pandas DataFrame from a dictionary where keys are column names and values are lists of equal length?
easy
A. The DataFrame will have rows named after the keys and columns from the list values.
B. A DataFrame is created with columns named after the keys and rows from the list values.
C. An error occurs because dictionaries cannot be converted to DataFrames.
D. Only the first key-value pair is used to create the DataFrame.
Solution
Step 1: Understand dictionary to DataFrame mapping
In pandas, dictionary keys become column names and values (lists) become column data.
Step 2: Check list lengths
All lists must have the same length to form rows correctly.
Final Answer:
A DataFrame is created with columns named after the keys and rows from the list values. -> Option B
Quick Check:
Dictionary keys = columns, lists = rows [OK]
Hint: Keys become columns, lists become rows if lengths match [OK]
Common Mistakes:
Thinking dictionary keys become row labels
Assuming only one key-value pair is used
Believing dictionaries can't create DataFrames
2. Which of the following is the correct syntax to create a pandas DataFrame from a dictionary named data?
easy
A. df = pd.DataFrame(data)
B. df = pd.DataFrame.from_dict(data, orient='index')
C. df = pd.DataFrame(data, columns=['A', 'B'])
D. df = pd.DataFrame(data).to_dict()
Solution
Step 1: Recall basic DataFrame creation syntax
The standard way to create a DataFrame from a dictionary is using pd.DataFrame(data).
Step 2: Analyze other options
df = pd.DataFrame.from_dict(data, orient='index') changes orientation to index, which is not default; df = pd.DataFrame(data, columns=['A', 'B']) tries to specify columns but may cause errors if columns don't match; df = pd.DataFrame(data).to_dict() converts DataFrame back to dict, not creation.
Final Answer:
df = pd.DataFrame(data) -> Option A
Quick Check:
Use pd.DataFrame(dictionary) to create DataFrame [OK]
Hint: Use pd.DataFrame(your_dict) for quick DataFrame creation [OK]
Common Mistakes:
Using .to_dict() instead of creating DataFrame
Confusing orient parameter for simple dict
Specifying columns that don't exist in dict
3. What will be the output of the following code?
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
medium
A. A DataFrame with rows named 'Name' and 'Age' and columns 0 and 1.
B. A dictionary printed with keys 'Name' and 'Age'.
C. An error because lists have different lengths.
D. A DataFrame with columns 'Name' and 'Age' and two rows with values Alice-25 and Bob-30.
Solution
Step 1: Create DataFrame from dictionary
The dictionary keys become columns 'Name' and 'Age'. The lists provide row data.
Step 2: Print DataFrame
Output shows two rows: first with 'Alice' and 25, second with 'Bob' and 30.
Final Answer:
A DataFrame with columns 'Name' and 'Age' and two rows with values Alice-25 and Bob-30. -> Option D