7 ways to create a Pandas DataFrame in Python

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

There are several ways to create a Pandas DataFrame. Here are a few examples:

From a dictionary

import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)

From a list of lists

data = [{'Name': 'Alice', 'Age': 25}, {'Name': 'Bob', 'Age': 30}, {'Name': 'Charlie', 'Age': 35}]
df = pd.DataFrame(data)

From a 2D Numpy array

import numpy as np
data = np.array([['Alice', 25], ['Bob', 30], ['Charlie', 35]])
df = pd.DataFrame(data, columns=['Name', 'Age'])

From a CSV file:

df = pd.read_csv('data.csv')

From an excel file

df = pd.read_excel('data.xlsx')

From a SQL query

import sqlite3

conn = sqlite3.connect('database.db')
df = pd.read_sql_query("SELECT * FROM table", conn)

You can also specify the index of DataFrame while creating it.

df = pd.DataFrame(data, columns=['Name', 'Age'], index=[1,2,3])

You can also create an empty DataFrame using the pd.DataFrame() constructor and later add data to it using the various DataFrame methods like loc[], iloc[], append(), insert(), concat() etc.

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.