Seriesのベクトル化演算
2つのSeries間で算術演算を行い、インデックスの自動アライメントを扱い、対応する値がない場合はNaNで埋めます。
「Seriesのベクトル化演算」はCoddyKit上の無料Pandas & NumPy Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPandas & NumPy Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Pandas & NumPy Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Vectorized Operations?
Like NumPy, Pandas Series support vectorized operations that apply arithmetic, comparisons, and functions to every element without Python loops. This provides both conciseness and speed. Under the hood each operation delegates to NumPy's C-level routines, but Pandas adds a critical bonus: automatic index alignment before performing the operation.
import pandas as pd
revenue = pd.Series([1000, 2000, 1500], index=['Jan', 'Feb', 'Mar'])
tax_rate = 0.2
net = revenue * (1 - tax_rate) # vectorized -- no loop
print(net)Arithmetic Between Two Series
When you add, subtract, multiply, or divide two Series, Pandas aligns them by index label first. Only positions where both Series have a label produce a result; unmatched labels produce NaN. This behaviour prevents hidden bugs from mis-aligned data — for example, adding January figures from two data sources that happen to be stored in different orders.
import pandas as pd
a = pd.Series([10, 20, 30], index=['x', 'y', 'z'])
b = pd.Series([1, 2, 3], index=['y', 'z', 'w'])
print(a + b)
# w NaN
# x NaN
# y 21.0
# z 32.0Filling NaN in Aligned Operations
To avoid NaN propagation in misaligned operations, use the arithmetic methods like .add(), .sub(), .mul(), and .div() with the fill_value= parameter. This substitutes a given value for any label that is missing in one of the Series before performing the operation — commonly 0 for addition and 1 for multiplication.
import pandas as pd
a = pd.Series([10, 20, 30], index=['x', 'y', 'z'])
b = pd.Series([1, 2, 3], index=['y', 'z', 'w'])
result = a.add(b, fill_value=0)
print(result)
# w 3.0
# x 10.0
# y 21.0
# z 32.0Scalar Arithmetic on a Series
Arithmetic with a scalar applies the operation to every element and produces a new Series with the same index. This is the simplest form of vectorization: s + 5, s * 100, s ** 2. Scalar arithmetic never introduces NaN and is used for unit conversion, normalisation, and offsetting time series.
import pandas as pd
prices_eur = pd.Series([1.0, 2.5, 4.0], index=['a', 'b', 'c'])
exchange = 1.08
prices_usd = prices_eur * exchange
print(prices_usd.round(2))
# a 1.08
# b 2.70
# c 4.32Applying NumPy ufuncs to Series
NumPy ufuncs work seamlessly on Pandas Series and return a Series with the same index. This means you can call np.sqrt(s), np.log1p(s), or np.exp(s) directly on a Series without converting to an array first. The result is a new Series, preserving the original labels.
import pandas as pd
import numpy as np
s = pd.Series([0, 1, 4, 9, 16], index=list('abcde'))
print(np.sqrt(s))
# a 0.0
# b 1.0
# c 2.0
# d 3.0
# e 4.0Comparison Operations on Series
Comparison operators (==, !=, >, <, >=, <=) return a boolean Series element-wise. These are the building blocks for filtering: you pass the boolean Series to .loc to select the matching rows. Pandas also aligns by index when comparing two Series.
import pandas as pd
s = pd.Series([3, 7, 2, 9, 1], index=list('abcde'))
print(s > 4)
# a False
# b True
# c False
# d True
# e False
print(s[s > 4]) # b:7 d:9Chaining Vectorized Operations
Because each operation returns a new Series, you can chain multiple transformations in a single expression. This is readable and avoids creating unnecessary intermediate variables. For complex pipelines, break the chain across lines using parentheses. Chaining is the Pandas equivalent of functional programming with immutable data.
import pandas as pd
import numpy as np
raw_scores = pd.Series([55, 80, 45, 90, 72])
normalised = (raw_scores - raw_scores.min()) / (raw_scores.max() - raw_scores.min())
print(normalised.round(2))
# 0 0.22
# 1 0.78
# 2 0.00
# 3 1.00
# 4 0.60Handling NaN in Vectorized Operations
NaN is contagious: any arithmetic involving NaN produces NaN. Use s.fillna(value) to substitute before operating, or use the skipna=True parameter on aggregation methods. The pd.isna(s) function returns a boolean Series marking NaN positions — use it to audit data before computing statistics.
import pandas as pd
import numpy as np
s = pd.Series([1.0, np.nan, 3.0, np.nan, 5.0])
print(s + 10)
# 0 11.0 2 13.0 4 15.0 -- NaN stays NaN
print(s.fillna(0) + 10)
# 0 11.0 1 10.0 2 13.0 3 10.0 4 15.0abs(), clip(), and round() on Series
Pandas Series expose many NumPy-like methods directly: s.abs(), s.clip(lower, upper), and s.round(decimals) all return new Series with the transformation applied element-wise. These are equivalent to the NumPy ufunc calls but use method syntax and preserve the Series index and name.
import pandas as pd
s = pd.Series([-2.5, 1.3, -0.7, 4.9, -3.1])
print(s.abs()) # [2.5 1.3 0.7 4.9 3.1]
print(s.clip(-2, 2)) # [-2. 1.3 -0.7 2. -2. ]
print(s.round(0)) # [-2. 1. -1. 5. -3.]pct_change() for Growth Rates
s.pct_change() computes the percentage change between consecutive elements: (current - previous) / previous. The first element is NaN since there is no previous value. This is the standard operation for computing month-over-month growth rates, daily returns in finance, and rate-of-change features for time series models.
import pandas as pd
monthly_revenue = pd.Series([100, 120, 110, 140],
index=['Q1', 'Q2', 'Q3', 'Q4'])
growth = monthly_revenue.pct_change()
print(growth.round(3))
# Q1 NaN
# Q2 0.200
# Q3 -0.083
# Q4 0.273cumsum() and cumprod() on Series
s.cumsum() returns a Series of running totals and s.cumprod() returns running products. These are used to compute cumulative revenue, compound investment returns, or running word counts. They preserve the original index labels, making it easy to plot the cumulative series over time or category.
import pandas as pd
daily_sales = pd.Series([100, 150, 80, 200],
index=['Mon', 'Tue', 'Wed', 'Thu'])
print(daily_sales.cumsum())
# Mon 100
# Tue 250
# Wed 330
# Thu 530Quick Check
Test your understanding of vectorized operations on Series from this lesson.
Lesson Recap
In this lesson you learned: arithmetic operators apply element-wise to Series without loops, Pandas automatically aligns two Series by index label before operating, placing NaN at unmatched positions, and methods like fill_value, pct_change, and cumsum extend vectorized operations for analytical tasks. Next up we explore useful Series methods like describe, value_counts, and map for rapid summarisation.
よくある質問
「Seriesのベクトル化演算」レッスンは無料ですか?
はい。「Seriesのベクトル化演算」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Pandas & NumPy Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Pandas & NumPy Academyコースには全4レッスンが含まれています。
「Seriesのベクトル化演算」で何を学びますか?
2つのSeries間で算術演算を行い、インデックスの自動アライメントを扱い、対応する値がない場合はNaNで埋めます。 ブラウザで直接実行するハンズオンコードでPandas & NumPy Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Pandas & NumPy Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのPandas & NumPy Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Seriesのベクトル化演算」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このPandas & NumPy Academyレッスンでコードを書いて実行できますか?
はい。すべてのPandas & NumPy Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Seriesの作成
- ラベルベースと位置ベースのアクセス
- Seriesのベクトル化演算
- 便利なSeriesメソッド