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

bookOperations 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?

正しい答えを選んでください

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.

すべての正しい答えを選択

question mark

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

Select all that apply.

すべての正しい答えを選択

question mark

What is the result of apples + oranges?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  10

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  10
some-alt