Creating a DataFrame is the first step to organize and analyze data easily. It helps turn raw data into a table format that is simple to work with.
You have data from a survey and want to analyze answers.
You collected sales numbers and want to find trends.
You want to clean and prepare data before making charts.
You need to combine data from different sources into one table.
You want to filter or sort data to find important information.
Syntax
Pandas
import pandas as pd
df = pd.DataFrame(data)
data can be a dictionary, list, or other formats.
The DataFrame looks like a table with rows and columns.
Examples
Create a DataFrame from a dictionary where keys are column names.
Pandas
import pandas as pd
data = {'Name': ['Anna', 'Bob'], 'Age': [28, 34]}
df = pd.DataFrame(data)
print(df)
Create a DataFrame from a list of lists and name the columns.
Pandas
import pandas as pd
data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=['A', 'B'])
print(df)
Sample Program
This program creates a simple table with names, ages, and cities. It then prints the table so you can see the organized data.
Pandas
import pandas as pd
# Create a DataFrame from a dictionary
people = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['NY', 'LA', 'Chicago']}
df = pd.DataFrame(people)
# Show the DataFrameprint(df)
OutputSuccess
Important Notes
DataFrames make it easy to select, filter, and change data.
Always check your data after creating a DataFrame to avoid mistakes.
Summary
DataFrames organize data into rows and columns like a spreadsheet.
Creating a DataFrame is the first step to analyze and visualize data.
They help you work with data in a clear and simple way.
Practice
(1/5)
1. Why is creating a DataFrame important when working with data in pandas?
easy
A. It converts data into images.
B. It automatically cleans all data errors.
C. It organizes data into rows and columns for easy analysis.
D. It deletes duplicate data without user input.
Solution
Step 1: Understand DataFrame structure
A DataFrame organizes data into rows and columns, similar to a spreadsheet.
Step 2: Recognize the benefit for analysis
This structure makes it easier to analyze and manipulate data clearly and simply.
Final Answer:
It organizes data into rows and columns for easy analysis. -> Option C
Quick Check:
DataFrames = organized data [OK]
Hint: DataFrames = tables that make data easy to handle [OK]
Common Mistakes:
Thinking DataFrames fix data errors automatically
Confusing DataFrames with image data
Assuming DataFrames delete duplicates by default
2. Which of the following is the correct way to create a pandas DataFrame from a dictionary?
easy
A. pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
B. pd.DataFrame['A': [1, 2], 'B': [3, 4]]
C. pd.DataFrame({'A' = [1, 2], 'B' = [3, 4]})
D. pd.DataFrame({'A': 1, 2, 'B': 3, 4})
Solution
Step 1: Check dictionary syntax
The dictionary keys must be strings and values lists, using colons ':' to assign.
Step 2: Confirm DataFrame constructor usage
pd.DataFrame() takes a dictionary with keys as column names and lists as column data.
Final Answer:
pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) -> Option A
Hint: Use curly braces and colons inside pd.DataFrame() [OK]
Common Mistakes:
Using square brackets instead of parentheses for DataFrame
Using '=' instead of ':' in dictionary
Passing single values instead of lists for column data
3. What will be the output of this code?
import pandas as pd
data = {'Name': ['Anna', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df.shape)
medium
A. (2, 3)
B. (2, 2)
C. (3, 2)
D. (1, 2)
Solution
Step 1: Understand DataFrame shape
DataFrame shape returns (rows, columns).
Step 2: Count rows and columns in data
Data has 2 names and 2 ages, so 2 rows and 2 columns.
Final Answer:
(2, 2) -> Option B
Quick Check:
Rows=2, Columns=2 [OK]
Hint: Shape = (rows, columns) count [OK]
Common Mistakes:
Confusing rows with columns count
Assuming shape returns columns first
Counting keys as rows
4. Identify the error in this DataFrame creation code:
import pandas as pd
data = {'X': [1, 2], 'Y': 3}
df = pd.DataFrame(data)
medium
A. Data values must be lists or arrays, not single integers.
B. Dictionary keys must be integers, not strings.
C. pd.DataFrame() cannot take a dictionary as input.
D. Missing import statement for pandas.
Solution
Step 1: Check data dictionary values
Values are single integers, but DataFrame expects list-like objects for columns.
Step 2: Understand DataFrame input requirements
Each column must have multiple values in a list or array to form rows.
Final Answer:
Data values must be lists or arrays, not single integers. -> Option A
Quick Check:
Values need to be lists [OK]
Hint: Use lists for column data, not single numbers [OK]
Common Mistakes:
Using single values instead of lists
Thinking keys must be integers
Ignoring import pandas statement
5. You have nested data: {'Team': ['A', 'B'], 'Scores': [[10, 20], [15, 25]]}. How do you create a DataFrame that shows each score as a separate row with the team name repeated?
hard
A. Convert nested lists to strings before creating the DataFrame.
B. Create DataFrame directly with nested lists; pandas will flatten automatically.
C. Use pd.DataFrame({'Team': ['A', 'B'], 'Scores': [10, 20, 15, 25]}).
D. Use pd.DataFrame.explode() on the 'Scores' column after creating the DataFrame.
Solution
Step 1: Create DataFrame with nested lists
Creating DataFrame with nested lists keeps 'Scores' as lists in cells.
Step 2: Use explode() to flatten lists into rows
explode() expands each list element into its own row, repeating 'Team' values.
Final Answer:
Use pd.DataFrame.explode() on the 'Scores' column after creating the DataFrame. -> Option D
Quick Check:
explode() flattens nested lists [OK]
Hint: Use explode() to turn list elements into rows [OK]
Common Mistakes:
Expecting pandas to flatten nested lists automatically
Manually flattening data without explode()
Converting lists to strings losing data structure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.