DataFrame basics
A DataFrame is a table with labelled rows and columns, like a spreadsheet inside Python.
In this page:
Syntax
df = pd.DataFrame({"column1": [value1, value2],
"column2": [value3, value4]})
DataFrame basics
A DataFrame is a two-dimensional labelled structure where each column is a Series that can have its own dtype. It is the workhorse of Pandas. The simplest way to create one is from a dictionary of columns.
Note:
Columns can hold different types, unlike a NumPy 2-D array.
Example: DataFrame basics
import pandas as pd
df = pd.DataFrame({
"name": ["Ann", "Bob", "Cy"],
"age": [28, 35, 41],
"member": [True, False, True],
})
print(df)
print(df.dtypes)
# Output:
# name age member
# 0 Ann 28 True
# 1 Bob 35 False
# 2 Cy 41 True
# name object
# age int64
# member bool
# dtype: object
Related Topics
Common Mistakes
- Confusing rows and columns when building from lists
- Expecting all columns to share a dtype
- Modifying a copy and expecting the original to change
Chapter Summary
- A DataFrame is a labelled 2-D table
- Columns are Series
- Each column has its own dtype
- Build it from a dict of columns
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: