In this collaborative coding exercise, weβll explore Pandas Series, a fundamental data structure in the Pandas library. Youβll work together to create, manipulate, and analyze Series objects.
Feel free to refer to this cheatsheet throughout the exercise if you need a quick reminder about syntax or functionality.
Setup
First, letβs import the necessary libraries and create a sample Series.
import pandas as pdimport numpy as np# Create a sample Seriesfruits = pd.Series(['apple', 'banana', 'cherry', 'date', 'elderberry'], name='Fruits')print(fruits)
0 apple
1 banana
2 cherry
3 date
4 elderberry
Name: Fruits, dtype: object
Exercise 1: Creating a Series
Work together to create a Series representing the prices of the fruits in our fruits Series.
# Your code here# Create a Series called 'prices' with the same index as 'fruits'# Use these prices: apple: $0.5, banana: $0.3, cherry: $1.0, date: $1.5, elderberry: $2.0
Exercise 2: Finding Maximum and Minimum Values
Collaborate to explore Series indexing methods:
Find the most expensive fruit using .idxmax()
Find the least expensive fruit using .idxmin()
Calculate the total price of all fruits using .sum()
Tip
.idxmax() returns the index (name) of the maximum value
.idxmin() returns the index (name) of the minimum value
.max() and .min() return the values themselves
# Your code here# 1. Find the most expensive fruit using .idxmax()# 2. Find the least expensive fruit using .idxmin()# 3. Calculate the total price using .sum()
Exercise 3: Basic Series Analysis
Work as a team to practice basic Series methods:
What is the average price of the fruits? (use .mean())
What is the price range? Calculate: maximum price minus minimum price
Tip
Use .max() and .min() to get the actual price values, then subtract them.
# Your code here# 1. Calculate the average price using .mean()# 2. Calculate the price range (max - min)
Conclusion
In this collaborative exercise, youβve practiced creating, manipulating, and analyzing Pandas Series. Youβve learned how to perform basic operations, apply conditions, and modify Series objects. These skills will be valuable as you work with more complex datasets in the future.
Discussion Questions
What advantages does using a Pandas Series offer compared to using a Python list or dictionary?
Can you think of a real-world scenario where you might use a Pandas Series instead of a DataFrame?
What challenges did you face while working with Series in this exercise, and how did you overcome them?
Discuss these questions with your team and share your insights.