logo

Creating a DataFrame from Scratch

The most common way to create a DataFrame is from a dictionary. Each key becomes a column name, and each value (a list) becomes the column data.

import pandas as pd

df = pd.DataFrame({
    'product': ['Widget', 'Gadget'],
    'price': [10.99, 24.99]
})

You can also create from a list of dictionaries, where each dict is a row:

data = [
    {'name': 'Alice', 'age': 25},
    {'name': 'Bob', 'age': 30}
]
df = pd.DataFrame(data)

This is handy when you're building data row by row, like when processing API responses.

For more DataFrame creation patterns, see The Ultimate Pandas Bootcamp.