0
0
Data Analysis Pythondata~5 mins

Series arithmetic and alignment in Data Analysis Python

Choose your learning style9 modes available
Introduction

We use series arithmetic and alignment to do math on data that has labels. It helps us add, subtract, or compare data even if the labels don't match exactly.

You want to add sales numbers from two different months where some products are missing in one month.
You need to subtract expenses from income for different categories that don't fully overlap.
You want to combine survey results from two groups where some questions were only asked in one group.
You want to multiply prices and quantities for items where some items are missing in one list.
Syntax
Data Analysis Python
result = series1 + series2
result = series1 - series2
result = series1 * series2
result = series1 / series2

Series are like lists with labels (indexes).

When you do arithmetic, pandas matches data by these labels automatically.

Examples
Add two series with some matching and some different labels. The result shows sums where labels match and NaN where they don't.
Data Analysis Python
import pandas as pd

s1 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
s2 = pd.Series([1, 2, 3], index=['a', 'b', 'd'])

result = s1 + s2
print(result)
Multiply two series with the same labels. The result is element-wise multiplication.
Data Analysis Python
s1 = pd.Series([5, 10, 15], index=['x', 'y', 'z'])
s2 = pd.Series([2, 4, 6], index=['x', 'y', 'z'])

result = s1 * s2
print(result)
Subtract series where one has fewer labels. Missing labels result in NaN.
Data Analysis Python
s1 = pd.Series([100, 200], index=['p', 'q'])
s2 = pd.Series([50], index=['p'])

result = s1 - s2
print(result)
Sample Program

This program adds sales numbers from January and February. It shows how pandas adds values with matching labels and puts NaN where a label is missing in one series.

Data Analysis Python
import pandas as pd

# Create two series with some overlapping and some unique labels
sales_jan = pd.Series([250, 150, 400], index=['apples', 'bananas', 'oranges'])
sales_feb = pd.Series([200, 180, 100, 300], index=['apples', 'bananas', 'pears', 'oranges'])

# Add sales from January and February
combined_sales = sales_jan + sales_feb

print(combined_sales)
OutputSuccess
Important Notes

If a label is missing in one series, the result for that label is NaN (not a number).

You can use methods like .fillna() to replace NaN with a number if needed.

Arithmetic operations align data by labels automatically, no need to sort or match manually.

Summary

Series arithmetic matches data by labels before doing math.

Results have NaN where labels don't match.

This makes combining and comparing labeled data easy and safe.