Creating DataFrames
Construct DataFrames from dicts of lists, lists of dicts, and NumPy arrays, then inspect shape, columns, and dtypes.
What Is a DataFrame?
A DataFrame is a two-dimensional, size-mutable, labelled data structure — essentially a table with rows and columns, similar to a spreadsheet or SQL table. Each column is a Pandas Series sharing the same row index. DataFrames can hold columns of different dtypes, making them perfect for real-world datasets that mix numbers, text, and dates.
import pandas as pd
df = pd.DataFrame({'name': ['Alice', 'Bob', 'Carol'],
'age': [30, 25, 35],
'score': [88.5, 72.0, 95.3]})
print(df)Creating from a Dict of Lists
The most common way to construct a DataFrame is from a dict of lists: the keys become column names and the lists become column values. All lists must be the same length. The row index defaults to 0, 1, 2, ... unless you specify one with index=. This pattern mirrors how CSV data is often represented in Python.
import pandas as pd
df = pd.DataFrame({
'city': ['Berlin', 'Paris', 'Rome'],
'pop': [3.6, 2.2, 2.8],
'country': ['DE', 'FR', 'IT']
})
print(df.shape) # (3, 3)
print(df.dtypes)