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:
123456789101112import 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:
123456789101112import 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:
123456789101112import 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:
12345678910111213141516171819202122232425262728import 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
?
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 2.7
Operations over Series
Scorri per mostrare il menu
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:
123456789101112import 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:
123456789101112import 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:
123456789101112import 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:
12345678910111213141516171819202122232425262728import 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
?
Grazie per i tuoi commenti!