- Create a Series from a list, a dictionary and a scalar value.
- Use custom index labels and access values with
[],locandiloc. - Perform vectorised operations and filtering on a Series.
- Use common Series methods for statistics and missing values.
What is a Series?
A Series is a one-dimensional labelled array. It has values and an index (labels). If you do not give labels, pandas uses 0, 1, 2 ….
pd.Series(data, index=labels, name=...)import pandas as pd s = pd.Series([10, 20, 30, 40]) print(s) print(s[2]) print(s.index.tolist(), s.values.tolist())
0 10 1 20 2 30 3 40 dtype: int64 30 [0, 1, 2, 3] [10, 20, 30, 40]
import pandas as pd temps = pd.Series([31.5, 29.0, 33.2], index=["Mon", "Tue", "Wed"], name="Temp") print(temps) print(temps["Tue"])
Mon 31.5 Tue 29.0 Wed 33.2 Name: Temp, dtype: float64 29.0
import pandas as pd
seats = {"BCA": 60, "BBA": 40, "MCA": 30} # sample data
s = pd.Series(seats)
print(s)
print(pd.Series(seats, index=["MCA", "BCA"])) # choose labels
print(pd.Series(5, index=["a", "b", "c"]).tolist()) # scalar repeatedBCA 60 BBA 40 MCA 30 dtype: int64 MCA 30 BCA 60 dtype: int64 [5, 5, 5]
Accessing Values: loc and iloc
| Accessor | Selects by | Slice end |
|---|---|---|
s.loc[...] | Index label | Included |
s.iloc[...] | Integer position (0, 1, 2 …) | Excluded (like Python) |
import pandas as pd marks = pd.Series([82, 67, 91, 74], index=["Asha", "Ravi", "Meera", "John"]) print(marks.loc["Meera"], marks.iloc[0], marks.iloc[-1]) print(marks.loc["Ravi":"John"].tolist()) # label slice: end included print(marks.iloc[1:3].tolist()) # position slice: end excluded print(marks.loc[["Asha", "John"]].tolist())
91 82 74 [67, 91, 74] [67, 91] [82, 74]
Operations and Filtering
Operations apply to every value at once (vectorised). When two Series are combined, values are aligned by index label; labels found in only one Series give NaN (missing).
import pandas as pd marks = pd.Series([82, 67, 91, 74], index=["Asha", "Ravi", "Meera", "John"]) print((marks + 5).tolist()) print(marks[marks >= 75]) print((marks >= 75).sum(), "students scored 75 or more") a = pd.Series([1, 2, 3], index=["x", "y", "z"]) b = pd.Series([10, 20, 30], index=["y", "z", "w"]) print(a + b)
[87, 72, 96, 79] Asha 82 Meera 91 dtype: int64 2 students scored 75 or more w NaN x NaN y 12.0 z 23.0 dtype: float64
Useful Series Methods
import pandas as pd sales = pd.Series([120, 340, 90, 340, 210]) print(sales.sum(), sales.mean(), sales.max(), sales.min()) print(sales.median(), sales.idxmax()) print(sales.sort_values(ascending=False).tolist()) print(sales.value_counts().loc[340]) # how many times 340 occurs print(sales.nunique(), sales.size)
1100 220.0 340 90 210.0 1 [340, 340, 210, 120, 90] 2 4 5
import pandas as pd import numpy as np s = pd.Series([4.0, np.nan, 7.0, np.nan]) print(s.isna().tolist()) print(s.count(), s.size) # count() ignores missing values print(s.fillna(0).tolist()) print(s.dropna().tolist()) print(s.mean()) # NaN skipped automatically
[False, True, False, True] 2 4 [4.0, 0.0, 7.0, 0.0] [4.0, 7.0] 5.5
Key Takeaways
- A Series = values + index labels; it can be built from a list, dict or scalar.
locuses labels (slice end included);ilocuses positions (end excluded).- Operations are vectorised and aligned by label; mismatched labels give NaN.
Video : Pandas Series
Question / Answer (15 questions)
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- pandas development team. (n.d.). Intro to data structures. pandas User Guide. https://pandas.pydata.org/docs/user_guide/dsintro.html
- pandas development team. (n.d.). Indexing and selecting data. pandas User Guide. https://pandas.pydata.org/docs/user_guide/indexing.html
- pandas development team. (n.d.). Working with missing data. pandas User Guide. https://pandas.pydata.org/docs/user_guide/missing_data.html
- pandas development team. (n.d.). 10 minutes to pandas. pandas User Guide. https://pandas.pydata.org/docs/user_guide/10min.html
- McKinney, W. (2022). Python for Data Analysis (3rd ed.). O’Reilly Media. (Open-access online edition.) https://wesmckinney.com/book/