0
0
Data Analysis Pythondata~5 mins

Creating DataFrames (dict, list, CSV) in Data Analysis Python

Choose your learning style9 modes available
Introduction

We create DataFrames to organize and analyze data easily in tables. DataFrames help us see and work with data like a spreadsheet.

You have data in a dictionary and want to analyze it as a table.
You have a list of lists or list of records and want to turn it into a table.
You want to load data from a CSV file to explore or analyze it.
You want to combine data from different sources into one table.
You want to prepare data for charts or reports.
Syntax
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.

Examples
This creates an empty DataFrame with no columns or rows.
Data Analysis Python
import pandas as pd

# Empty dictionary
empty_dict = {}
df_empty = pd.DataFrame(empty_dict)
print(df_empty)
DataFrame with one row and one column.
Data Analysis Python
import pandas as pd

# Dictionary with one element
data_one = {'Name': ['Alice']}
df_one = pd.DataFrame(data_one)
print(df_one)
DataFrame created from a list with one row.
Data Analysis Python
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)
Loads data from a CSV file with headers.
Data Analysis Python
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)
Sample Program

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.

Data Analysis Python
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)
OutputSuccess
Important Notes

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.

Summary

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.