Menu
CoddyTech

Series

Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 31 of 60.

This chapter ends with a whole movie file, summed up in four lines:

A small table of 4 cartoons with their title, rating and votes, and an arrow to a card of four lines: Movies 4, ratings run from 6.4 to 7.7, average rating 7.05, half the movies have over 16600 votes

It starts small. A list knows values by position; pandas, the library for tables, gives each a label:

A Series drawn as one column: the labels apple, banana and mango at the side, their prices 1.5, 0.5 and 2.5 beside them, and the mango row highlighted

That labelled column is a Series. Give pd.Series the values and, with index=, the labels:

import pandas as pd

prices = pd.Series([1.5, 0.5, 2.5], index=["apple", "banana", "mango"])
print(prices)
print(prices["mango"])

This prints the labelled values, their type, then the mango's price:

apple     1.5
banana    0.5
mango     2.5
dtype: float64
2.5

prices.mean() is 1.5, as on an array. Without index=, the labels are 0, 1, 2.

challenge icon

Challenge

Medium

A ferry crosses three times a day. The program reads the passengers on the early, midday and late crossing, then a crossing's name into trip.

  1. Make a Series passengers of the three counts, labelled early, midday and late.
  2. Print it.
  3. Print the average, rounded to 1 decimal.
  4. Print the passengers on trip.

For 40, 95, 61 and early:

early     40
midday    95
late      61
dtype: int64
Average: 65.3 passengers
early: 40 passengers

Try it yourself

import pandas as pd

early = int(input())
midday = int(input())
late = int(input())
trip = input()

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler