We create DataFrames to organize and analyze data easily in tables. DataFrames help us see and work with data like a spreadsheet.
Creating DataFrames (dict, list, CSV) in Data Analysis Python
import pandas as pd # From dictionary data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]} df = pd.DataFrame(data) # From list of lists data_list = [['Alice', 25], ['Bob', 30]] df2 = pd.DataFrame(data_list, columns=['Name', 'Age']) # From CSV file df3 = pd.read_csv('filename.csv')
Use pd.DataFrame() to create a DataFrame from dict or list.
Use pd.read_csv() to load data from a CSV file.
import pandas as pd # Empty dictionary empty_dict = {} df_empty = pd.DataFrame(empty_dict) print(df_empty)
import pandas as pd # Dictionary with one element data_one = {'Name': ['Alice']} df_one = pd.DataFrame(data_one) print(df_one)
import pandas as pd # List with one row data_list_one = [['Alice', 25]] df_list_one = pd.DataFrame(data_list_one, columns=['Name', 'Age']) print(df_list_one)
import pandas as pd # CSV file with header # Assume 'people.csv' content: # Name,Age # Alice,25 # Bob,30 df_csv = pd.read_csv('people.csv') print(df_csv)
This program shows three ways to create DataFrames: from a dictionary, from a list of lists, and from a CSV file. It prints each DataFrame to see the data.
import pandas as pd # Create DataFrame from dictionary data_dict = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]} df_dict = pd.DataFrame(data_dict) print('DataFrame from dictionary:') print(df_dict) # Create DataFrame from list of lists data_list = [['Alice', 25], ['Bob', 30], ['Charlie', 35]] df_list = pd.DataFrame(data_list, columns=['Name', 'Age']) print('\nDataFrame from list of lists:') print(df_list) # Create a sample CSV file csv_content = 'Name,Age\nAlice,25\nBob,30\nCharlie,35' with open('sample_people.csv', 'w') as file: file.write(csv_content) # Load DataFrame from CSV df_csv = pd.read_csv('sample_people.csv') print('\nDataFrame loaded from CSV file:') print(df_csv)
Creating a DataFrame from a dictionary or list is very fast and uses little memory.
Loading from CSV depends on file size; large files take longer.
Common mistake: forgetting to specify column names when creating from a list.
Use dictionary input when you have named columns; use list input when you have row data without names.
DataFrames organize data in tables for easy analysis.
You can create DataFrames from dictionaries, lists, or CSV files.
Always check your data after creating to make sure it looks right.
Practice
Solution
Step 1: Understand what a DataFrame is
A DataFrame is like a table with rows and columns, used to organize data clearly.Step 2: Identify the purpose of DataFrames
They help analyze data easily by structuring it in rows and columns.Final Answer:
To organize data in a table format for easy analysis -> Option AQuick Check:
DataFrame = Table for data analysis [OK]
- Confusing DataFrame with a simple list
- Thinking DataFrame is for saving files only
- Believing DataFrame is just for calculations
Solution
Step 1: Recall pandas DataFrame creation syntax
Use pd.DataFrame() with a dictionary where keys are column names and values are lists of column data.Step 2: Check each option
pd.DataFrame({'name': ['Alice', 'Bob'], 'age': [25, 30]}) correctly uses a dictionary with lists for columns. Others have syntax errors or wrong formats.Final Answer:
pd.DataFrame({'name': ['Alice', 'Bob'], 'age': [25, 30]}) -> Option BQuick Check:
Dictionary with lists = pd.DataFrame() [OK]
- Using pd.DataFrame with wrong brackets
- Passing single values instead of lists
- Trying to assign columns after creating DataFrame
import pandas as pd data = [[1, 'Alice'], [2, 'Bob']] df = pd.DataFrame(data, columns=['ID', 'Name']) print(df)
Solution
Step 1: Understand DataFrame creation from list of lists
Passing a list of lists with columns names creates a DataFrame with those columns and rows.Step 2: Check the print output
The DataFrame will show two rows with columns 'ID' and 'Name' filled with given data.Final Answer:
A DataFrame with columns 'ID' and 'Name' showing the two rows of data -> Option AQuick Check:
List + columns = DataFrame table [OK]
- Expecting a list output instead of DataFrame
- Forgetting columns parameter causes default columns
- Confusing list of lists with dictionary input
import pandas as pd
df = pd.read_csv('data.csv', header=2)
print(df.head())Solution
Step 1: Understand header parameter in read_csv
header=2 means pandas treats the third row as column names, which may be wrong if first row has headers.Step 2: Identify correct header usage
Usually header=0 is used to read the first row as column names. Using 2 can cause wrong column names or data misalignment.Final Answer:
The header parameter should be 0 to read the first row as column names -> Option CQuick Check:
header=0 reads first row as columns [OK]
- Setting header to wrong row number
- Confusing file extensions
- Misunderstanding print(df.head()) usage
Solution
Step 1: Read CSV file into DataFrame
Use pd.read_csv('file.csv') to load all data.Step 2: Filter rows where 'Score' > 80
Use boolean indexing: df[df['Score'] > 80] to keep only rows with Score above 80.Final Answer:
import pandas as pd\ndf = pd.read_csv('file.csv')\ndf_filtered = df[df['Score'] > 80]\nprint(df_filtered) -> Option DQuick Check:
Boolean indexing filters rows [OK]
- Trying to filter inside read_csv
- Using df.filter() incorrectly
- Using df.where() without proper condition
Ready to prove it?
Pass the test and this pattern moves unknown
