What does the align() method do in the pandas series?


The pandas Series align method is used to align two pandas series objects on basics of the same row and/or column configuration, which is done by specifying the parameters like join, axis, etc.

Instead of combining the two series of objects, the pandas series align method aligns them in a specific order. This method takes 10 parameters which are “other, join='outer', axis=None, level=None, copy=True, fill_value=None, method=None, limit=None, fill_axis=0, broadcast_axis=None”. Out of these parameters other, join and axis parameters are very important. Based on these parameters the output series object alignment depends.

Example 1

import pandas as pd
s1 = pd.Series([8,4,2,1], index=[5,3,4,2])
s2 = pd.Series([15,12,10,11],index=[1,2,4,5])
print(s1)
print(s2)
a,b = s1.align(s2)
print("Output for align method")
print(a)
print(b)

Explanation

The s1 and s2 are the two pandas series objects with index labels [1,2,4,5] and [2,3,4,5] respectively. We have applied the align method on these two series objects without any parameters, and we got another two series objects as an output of this align method.

Output

5    8
3    4
4    2
2    1
dtype: int64
1    15
2    12
4    10
5    11
dtype: int64

Output of align method without any parameter.

1    NaN
2    1.0
3    4.0
4    2.0
5    8.0
dtype: float64
1    15.0
2    12.0
3    NaN
4    10.0
5    11.0
dtype: float64

The above 4 series objects are s1, s2, a, and b. The top 2 objects are s1 and s2, and the bottom two are generated from the pandas series align method with default parameters.

The index labels in s1 have been rearranged so they align with the indexes in s2.

An index labeled '1' has been added to s1, and an index labeled '3' has been added to s2. These values have been filled with NaN. This is because the default join parameter is an outer join on the index labels.

Example 2

import pandas as pd
s1 = pd.Series([8,4,2,1], index=[5,3,4,2])
s2 = pd.Series([15,12,10,11],index=[1,2,4,5])
print(s1)
print(s2)
a,b = s1.align(s2, join='right')
print("Output of align method with join parameter.")
print(a)
print(b)

Explanation

Now we have applied the join parameter with the ‘right’ option to the same above example. And observe the difference in the below output block.

Output

5    8
3    4
4    2
2    1
dtype: int64
1    15
2    12
4    10
5    11
dtype: int64

Output of align method with join parameter.

1    NaN
2    1.0
4    2.0
5    8.0
dtype: float64
1    15
2    12
4    10
5    11
dtype: int64

Only the rows that are found in the "right" series object (s2) are retained. And the index label “3” is no longer present. This is because we made a right join on the series objects.

Updated on: 09-Mar-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements