Data Science · Level 2 Series

pandas Series

A Series is a column of values with labels. Read it by label or by position without mixing the two up.

  • pd.Series(values, index=labels)
  • s["label"] reads by label
  • s.iloc[0] reads by position

A Series is a column of values with labels, its index. s["Mon"] reads by label and s.iloc[0] by position. Trap: with number labels, s[0] means "the label 0", which isn't always the first row. Use iloc.

import pandas as pd

visits = pd.Series([120, 340, 90], index=["Mon", "Tue", "Wed"])
visits["Tue"]     # 340
visits.iloc[-1]   # 90

Write first_and_last(s) that returns the first and last values of a Series, by position, as a tuple, for example on pd.Series([5, 6, 7], index=[2, 1, 0]) it returns (5, 7).

solution.py
Test Suites Pending
Output