Pandas DataFrame Align Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will explore the DataFrame.align() function in the Python Pandas library. The align() function allows us to align two DataFrames on their axes using different join methods like outer, inner, left, and right. This is useful when we want to synchronize data between two DataFrames or between a DataFrame and a Series.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/select_columns -.-> lab-68579{{"`Pandas DataFrame Align Function`"}} python/lists -.-> lab-68579{{"`Pandas DataFrame Align Function`"}} python/tuples -.-> lab-68579{{"`Pandas DataFrame Align Function`"}} python/importing_modules -.-> lab-68579{{"`Pandas DataFrame Align Function`"}} python/numerical_computing -.-> lab-68579{{"`Pandas DataFrame Align Function`"}} python/data_analysis -.-> lab-68579{{"`Pandas DataFrame Align Function`"}} python/build_in_functions -.-> lab-68579{{"`Pandas DataFrame Align Function`"}} end

Import the required libraries and create the DataFrames

First, let's import the pandas library and create two DataFrames with different indexes and columns. The first DataFrame, df1, will have the columns 'Name', 'Roll No', 'Subject', and 'Marks', and the second DataFrame, df2, will have the columns 'Name', 'Roll No', and 'Marks'.

import pandas as pd

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])

Align the DataFrames using "left" join on columns

Next, let's align the two DataFrames using the "left" join on columns. We will use the align() function and specify the join method as "left" and the axis as 1.

a1, a2 = df1.align(df2, join='left', axis=1)
print(a1)
print(a2)

Align the DataFrames using "right" join on columns

Similarly, we can align the DataFrames using the "right" join on columns. We will use the align() function and specify the join method as "right" and the axis as 1.

a1, a2 = df1.align(df2, join='right', axis=1)
print(a1)
print(a2)

Align the DataFrames using "outer" join on columns

We can also align the DataFrames using the "outer" join on columns. We will use the align() function and specify the join method as "outer" and the axis as 1.

a1, a2 = df1.align(df2, join='outer', axis=1)
print(a1)
print(a2)

Align the DataFrames using "inner" join on columns

Finally, we can align the DataFrames using the "inner" join on columns. We will use the align() function and specify the join method as "inner" and the axis as 1.

a1, a2 = df1.align(df2, join='inner', axis=1)
print(a1)
print(a2)

Summary

In this lab, we learned how to use the DataFrame.align() function in the Pandas library. We explored different join methods like outer, inner, left, and right to align two DataFrames on their columns or indexes. This function is useful when we want to synchronize data between different data sources or perform operations on aligned data.

Other Python Tutorials you may like