Pandas · Series and DataFrame
Series (a labelled 1-D array) and DataFrame (a table of Series sharing an index), and how index alignment works.
- A Series is a 1-D NumPy array plus an index (labels).
- A DataFrame is a table: a dict of Series (columns) that share one row index.
The important new idea is alignment. When you add two Series, pandas matches them by index label, not by position. A label present in only one of them gives NaN.
Useful first commands: df.head(), df.info(), df.describe(), df.shape, df.dtypes.
import pandas as pd
s = pd.Series([88, 79, 95], index=["maths", "physics", "python"], name="marks")
print(s)
print(s["python"], s.mean().round(2))
bonus = pd.Series({"python": 5, "maths": 2, "chemistry": 10})
print(s + bonus) # aligned by label
df = pd.DataFrame({
"name": ["Asha", "Ravi", "Sahaj", "Meet"],
"branch": ["CSE", "ICT", "ICT", "ME"],
"cgpa": [8.4, 7.1, 8.9, 6.8],
})
print(df)
print(df.shape, list(df.columns))
print(df.describe().round(2))
Output:
maths 88
physics 79
python 95
Name: marks, dtype: int64
95 87.33
chemistry NaN
maths 90.0
physics NaN
python 100.0
dtype: float64
name branch cgpa
0 Asha CSE 8.4
1 Ravi ICT 7.1
2 Sahaj ICT 8.9
3 Meet ME 6.8
(4, 3) ['name', 'branch', 'cgpa']
cgpa
count 4.00
mean 7.80
std 1.01
min 6.80
25% 7.02
50% 7.75
75% 8.52
max 8.90Sources
- 10 minutes to pandas — pandas
- Intro to data structures — pandas
- Python for Data Analysis, 3E (ch. 5) — Wes McKinney