pandas library
pandas is an open-source project, funded by nonprofit NumFOCUS.
pandas (imported as pd) is Python's standard library for tabular data — rows and columns, like a spreadsheet, with tools for filtering, sorting, and summarizing built in. It's a third-party package, not part of the standard library, and is built on top of NumPy.
Install
pip install pandas
Import
pd is the near-universal alias for pandas — used throughout this page and in virtually every codebase that imports it.
import pandas as pd
| Type | Shape | Like |
|---|---|---|
Series |
One column of labeled values | A single spreadsheet column |
DataFrame |
Many columns, sharing an index | A whole spreadsheet or table |
Building a DataFrame
A DataFrame is most often built from a list of dicts — one dict per row, with matching keys becoming the column names.
import pandas as pd
snakes = pd.DataFrame([
{"species": "ball", "length_ft": 4.5, "venomous": False},
{"species": "burmese", "length_ft": 12, "venomous": False},
])
print(snakes)
Building from a dict of lists
The same table can also be built from a single dict where each key maps to a whole column's worth of values. The rows line up by position across every list, so all the lists need to be the same length.
pd.DataFrame({
"species": ["ball", "burmese"],
"length_ft": [4.5, 12],
})
Run a building a DataFrame example
All the examples above, combined into one script:
import pandas as pd
snakes = pd.DataFrame([
{"species": "ball", "length_ft": 4.5, "venomous": False},
{"species": "burmese", "length_ft": 12, "venomous": False},
])
print(snakes)
import pandas as pd
snakes = pd.DataFrame({
"species": ["ball", "burmese"],
"length_ft": [4.5, 12],
})
print(snakes)
Working with a DataFrame
A single column pulled out of a DataFrame (with df["column"]) is a Series — comparing it to a value produces a boolean mask, exactly like a NumPy array, for filtering rows.
import pandas as pd
snakes = pd.DataFrame([
{"species": "ball", "length_ft": 4.5},
{"species": "burmese", "length_ft": 12},
{"species": "boa", "length_ft": 8},
])
big_snakes = snakes[snakes["length_ft"] > 5]
print(big_snakes)
Sorting rows
Returns the DataFrame reordered by a column. .sort_values("column") sorts ascending by default, or descending with ascending=False. Like most pandas operations, it returns a new DataFrame rather than reordering the original in place.
snakes.sort_values("length_ft", ascending=False) # rows reordered longest-first
Summarizing a column
Calling .mean(), .max(), or similar directly on a column summarizes it down to a single number. The same way it would on a NumPy array — a Series supports the same aggregation methods.
snakes["length_ft"].mean() # 8.166666666666666
snakes["length_ft"].max() # 12.0
Run a working with a DataFrame example
All the examples above, combined into one script:
import pandas as pd
snakes = pd.DataFrame([
{"species": "ball", "length_ft": 4.5},
{"species": "burmese", "length_ft": 12},
{"species": "boa", "length_ft": 8},
])
big_snakes = snakes[snakes["length_ft"] > 5]
print(big_snakes)
import pandas as pd
snakes = pd.DataFrame([
{"species": "ball", "length_ft": 4.5},
{"species": "burmese", "length_ft": 12},
{"species": "boa", "length_ft": 8},
])
print(snakes.sort_values("length_ft", ascending=False))
import pandas as pd
snakes = pd.DataFrame([
{"species": "ball", "length_ft": 4.5},
{"species": "burmese", "length_ft": 12},
{"species": "boa", "length_ft": 8},
])
print(snakes["length_ft"].mean())
print(snakes["length_ft"].max())