Course Content
Pandas: First Steps
Pandas: First Steps
Introduction to PandasSeriesTask: Coffee Orders SeriesTask: Barista's Favorites CoffeesAccessing Elements from a SeriesTask: Drink of the HourTask - Signature DrinksTask: Mid-Morning SpecialsTask - Fetching the Mid-Shift DrinksOperations over SeriesTask: Modifying a single elementTask: Modifying the Entire SeriesTask: Modifying Part of a SeriesTask: Processing the Restock of Inventory
Operations over Series
Modifying a Single Element
In Pandas, it's easily possible to perform operations over the Series. We can modify a single element by indexing it:
import pandas as pd # Create a Series of daily temperatures (in Β°C) temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) print("\nUpdating Thursday's temperature to 20Β°C...") temperatures["Thursday"] = 20 print(temperatures)
Modifying the Entire Series
Pandas makes it very simple to perform operations over all the elements of a Series:
import pandas as pd # Create a Series of daily temperatures (in Β°C) temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) print("\nIncreasing all temperatures by 2Β°C...") temperatures = temperatures + 2 print(temperatures)
We can perform operations over slices of the Series as well. This gives us control over how much data to change:
import pandas as pd # Create a Series of daily temperatures (in Β°C) temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) print("\nDecreasing temperatures between Wednesday and Friday by 1Β°C...") temperatures['Wednesday':'Friday'] -= 1 print(temperatures)
Operations on Series
Pandas also gives us the ability to perform arithmetic operations over the corresponding elements of two or more series with ease.
The following example finds the average between the temperatures of two cities:
import pandas as pd # Temperatures in City A (in Β°C) city_a = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) # Temperatures in City B (in Β°C) city_b = pd.Series( [20, 19, 24, 20, 22, 21, 19], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) # Add the two series to get total temperatures total_temperatures = city_a + city_b # Calculate average temperature average_temperatures = total_temperatures / 2 print("City A temperatures:") print(city_a) print("\nCity B temperatures:") print(city_b) print("\nAverage temperatures:") print(average_temperatures)
1. You have the following Series representing the number of books read each month. Which line correctly updates the number of books read in March to 6?
2. You want to increase all values in the following Series by 10
. Which line of code correctly applies this operation?
Select all that apply.
3. Which of the following lines reduces sales on Tuesday to Thursday by 10
?
Select all that apply.
4. What is the result of apples + oranges
?
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 10