Signup/Sign In
LAST UPDATED: MARCH 12, 2021

Pandas DataFrame align() Function

In this tutorial, we will learn the python pandas DataFrame.align() method. This method aligns two objects on their axes with the specified join method. This method is helpful when we want to synchronize a dataframe with another dataframe or a dataframe with a Series using different join methods like the outer, inner, left, and right.

Syntax

The syntax required to use this function is as follows.

DataFrame.align(other, join='outer', axis=None, level=None, copy=True, fill_value=None, method=None, limit=None, fill_axis=0, broadcast_axis=None)

Parameters:

other: It can be a DataFrame or Series.

join: 'outer', 'inner', 'left', 'right'. The default is 'outer' join.

axis: the allowed axis of the other object, default None. Align on the index (0), columns (1), or both (None).

level: int or level name, default None. Broadcast across a level, matching Index values on the passed MultiIndex level.

copy: bool, default True. Always returns new objects. If copy=False and no reindexing is required then original objects are returned.

fill_value: scalar, default np.NaN. Value to use for missing values. Defaults to NaN, but can be any “compatible” value.

method{‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None

limit: int, default None.

fill_axis {0 or ‘index’, 1 or ‘columns’}, default 0. Filling axis, method, and limit.

broadcast_axis {0 or ‘index’, 1 or ‘columns’}, default None. Broadcast values along this axis, if aligning two objects of different dimensions.

Example: Create and print the DataFrame

Create two dataframes with different indexes, columns and print the output.

df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
print("----------Printing DataFrame 1----------")
print(df1)
print("----------Printing DataFrame 2----------")
print(df2)

Once we run the program we will get the following result.


----------Printing DataFrame 1----------
Name Roll No Subject Marks
1 Abhishek 100 Science 90
2 Anurag 101 Science 85
----------Printing DataFrame 2----------
Name Roll No Marks
2 Chetan 103 75
3 Divya 104 80
4 Diya 105 92

Example: Align with Left Join on Columns(axis=1)

The below example shows how to align two different dataframes with left join on columns.

df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='left', axis=1)
print("---------After Aligning to the left DataFrame 1--------")
print(a1)
print("---------After Aligning to the left DataFrame 2--------")
print(a2)

Once we run the program we will get the following result.


---------After Aligning to the left DataFrame 1--------
Name Roll No Subject Marks
1 Abhishek 100 Science 90
2 Anurag 101 Science 85
---------After Aligning to the left DataFrame 2--------
Name Roll No Subject Marks
2 Chetan 103 NaN 75
3 Divya 104 NaN 80
4 Diya 105 NaN 92

Example: Align with right Join on Columns (axis=1)

The below example shows how to align two different dataframes with the right join on columns.

df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='right', axis=1)
print("---------After Aligning to the right DataFrame 1--------")
print(a1)
print("---------After Aligning to the right DataFrame 2--------")
print(a2)

Once we run the program we will get the following result.


---------After Aligning to the right DataFrame 1--------
Name Roll No Marks
1 Abhishek 100 90
2 Anurag 101 85
---------After Aligning to the right DataFrame 2--------
Name Roll No Marks
2 Chetan 103 75
3 Divya 104 80
4 Diya 105 92

Example: Align with outer Join on Columns(axis=1)

The below example shows how to align two different dataframes with outer join on columns.

df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='outer', axis=1)
print("---------After Aligning to the outer DataFrame 1--------")
print(a1)
print("---------After Aligning to the outer DataFrame 2--------")
print(a2)

Once we run the program we will get the following result.


---------After Aligning to the outer DataFrame 1--------
Marks Name Roll No Subject
1 90 Abhishek 100 Science
2 85 Anurag 101 Science
---------After Aligning to the outer DataFrame 2--------
Marks Name Roll No Subject
2 75 Chetan 103 NaN
3 80 Divya 104 NaN
4 92 Diya 105 NaN

Example: Align with inner Join on Columns(axis=1)

The below example shows how to align two different dataframes with left join on columns.

df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='inner', axis=1)
print("---------After Aligning to the inner DataFrame 1--------")
print(a1)
print("---------After Aligning to the inner DataFrame 2--------")
print(a2)

Once we run the program we will get the following result.


---------After Aligning to the inner DataFrame 1--------
Name Roll No Marks
1 Abhishek 100 90
2 Anurag 101 85
---------After Aligning to the inner DataFrame 2--------
Name Roll No Marks
2 Chetan 103 75
3 Divya 104 80
4 Diya 105 92

Example: Align with Left Join on the index(axis=0)

The below example shows how to align two different dataframes with left join on index.

df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'], index=[1,2])
df2 = pd.DataFrame([['Chetan',103,75], ['Divya',104,80], ['Diya',105,92]], columns=['Name', 'Roll No', 'Marks'], index=[2,3,4])
a1, a2 = df1.align(df2, join='left', axis=0)
print("---------After Aligning to the left with axis=0, DataFrame 1--------")
print(a1)
print("---------After Aligning to the left with axis=0, DataFrame 2--------")
print(a2)

Once we run the program we will get the following result.


---------After Aligning to the left with axis=0, DataFrame 1--------
Name Roll No Subject Marks
1 Abhishek 100 Science 90
2 Anurag 101 Science 85
---------After Aligning to the left with axis=0, DataFrame 2--------
Name Roll No Marks
1 NaN NaN NaN
2 Chetan 103.0 75.0

Conclusion

In this tutorial we learned the python pandas DataFrame.align() method. We understand the syntax, parameters, and solved examples by different join methods and axis.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.