Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Operations over Series | Getting Started
Pandas: First Steps
course content

Contenu du cours

Pandas: First Steps

Pandas: First Steps

1. Getting Started
2. Basics of Dataframes

book
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:

123456789101112
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)
copy

Modifying the Entire Series

Pandas makes it very simple to perform operations over all the elements of a Series:

123456789101112
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)
copy

We can perform operations over slices of the Series as well. This gives us control over how much data to change:

123456789101112
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)
copy

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:

12345678910111213141516171819202122232425262728
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)
copy

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?

question mark

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?

Select the correct answer

question mark

You want to increase all values in the following Series by 10. Which line of code correctly applies this operation?

Select all that apply.

Select the correct answer

question mark

Which of the following lines reduces sales on Tuesday to Thursday by 10?

Select all that apply.

Select the correct answer

question mark

What is the result of apples + oranges?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 10
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt