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:
It starts small. A list knows values by position; pandas, the library for tables, gives each a label:
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.5prices.mean() is 1.5, as on an array. Without index=, the labels are 0, 1, 2.
Challenge
MediumA 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.
- Make a Series
passengersof the three counts, labelledearly,middayandlate. - Print it.
- Print the average, rounded to 1 decimal.
- 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 passengersTry it yourself
import pandas as pd
early = int(input())
midday = int(input())
late = int(input())
trip = input()
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
7pandas DataFrames
SeriesDataFramesReading CSV FilesInspecting DataSelecting ColumnsRecap: Movie Ratings2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler