Pandas DataFrame align() Method


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame align()

The align() method aligns two (2) DataFrames/Series to ensure these DataFrames have identical row/column configurations. Use this method if you do not want the data altered.

The syntax for this method 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)
ParameterDescription
otherThis parameter can be a DataFrame/Series.
joinJoins on the: outer, inner, left, or right.
axisCan be aligned: index (0), columns (1), or both, which is None.
levelBroadcast over a level. Level matches the index value(s) on a multi-index level.
copyIf True, this parameter returns a copy of the original DataFrame/Series. If False, no re-indexing is needed. The original DataFrame/Series returns.
fill_valueIf there is missing data in the DataFrame/Series, the missing data, by default, is set as NaN, or any other valid value.
methodThis parameter can be: backfill, bfill, pad, ffill, or None by default.
limitIf this parameter contains data, it will fill according to the method selected for x number of NaN values.
fill_axisThis parameter can be: 0 or 'index', 1 or 'columns', or by default, 0.
broadcast_axis This parameter can be: 0 or 'index', 1 or 'columns', or by default, 0. This parameter broadcasts values along a specified axis.

For each join parameter of the align() method, the following two (2) DataFrames are referenced for the remainder of this article.

Original DataFrames

df_staff1 = pd.DataFrame([['Micah', 2100, 'Analyst', 32], 
                          ['Barb',  2101, 'Coder', 57]], 
                          columns=['EName', 'EID', 'Title', 'Age'], index=[1,2])

df_staff2 = pd.DataFrame([['Mark',   2102, 75], 
                          ['Jason',  2103, 80], 
                          ['Dakota', 2104, 92]], 
                          columns=['EName', 'EID', 'Age'], index=[3,4,5])

print(df_staff1)
print(df_staff2)
  • Line [1-2] creates DataFrames from a list of lists and sets the index. These save to df_staff1 and df_staff2.
  • Line [3] outputs the results to the terminal.

Output

df_staff1

 ENameEIDTitleAge
1Micah2100Analyst32
2Barb2101Coder57

df_staff2

 ENameEIDAge
3Mark210275
4Jason210380
5Dakota210492

Summary

  • The df_staff1 DataFrame contains four (4) labels ('EName', 'EID', 'Title', 'Age').
  • The df_staff2 DataFrame contains three (3) labels ('EName', 'EID', 'Age').
  • Both DataFrames have the 'EName', 'EID', and 'Age' labels in common.
  • The df_staff1 DataFrame has one (1) additional label: 'Title'.
  • With no join, parameter entered, both complete DataFrames output to the terminal.

DataFrame align() Inner Join

The align inner join parameter joins the column label(s) from the inner label(s). Notice the change(s) from the Original DataFrames.

ia1, ia2 = df_staff1.align(df_staff2, join='inner', axis=1)
print(ia1)
print(ia2)
  • Line [1] performs an inner join on the DataFrames.
  • Line [2-3] outputs the results to the terminal.

Output

df_staff1

 ENameEIDAge
1Micah210032
2Barb210157

df_staff2

 ENameEIDAge
3Mark210275
4Jason210380
5Dakota210492

Summary

  • Both DataFrames have the 'EName''EID',  and 'Age' labels in common.
  • Labels that appear in both DataFrames remain. Therefore 'Title' does not display.
  • No DataFrame values change.

DataFrame align() Outer Join

The align outer join parameter re-arranges the column label(s) on the outer label. Notice the change(s) from the Original DataFrames.

ia1, ia2 = df_staff1.align(df_staff2, join='outer', axis=1)
print(ia1)
print(ia2)
  • Line [1] performs an outer join on the DataFrames.
  • Line [2-3] outputs the results to the terminal.

Output

df_staff1

 AgeEIDENameTitle
1322100MicahAnalyst
2572101BarbCoder

df_staff2

 AgeEIDENameTitle
3752102MarkNaN
4802103JasonNaN
5922104DakotaNaN

Summary

  • The original DataFrame df_staff2 does not contain 'Title'. This label displays NaN values.
  • No DataFrame values have changed.

DataFrame align() Left Join

The align left join parameter joins the column label(s) on the left label. Notice the change(s) from the Original DataFrames.

ia1, ia2 = df_staff1.align(df_staff2, join='left', axis=1)
print(ia1)
print(ia2)
  • Line [1] performs a left join on the DataFrames.
  • Line [2-3] outputs the results to the terminal.

Output

df_staff1

 ENameEIDTitleAge
1Micah2100Analyst32
2Barb2101Coder57

df_staff2

 ENameEIDTitleAge
3Mark2102NaN75
4Jason2103NaN80
5Dakota2105NaN 

Summary

  • The original DataFrame df_staff2 does not contain 'Title'. This label displays NaN values.
  • No DataFrame values have changed.

DataFrame align() Right Join

The align right join parameter joins the column label(s) on the right label. Notice the change(s) from the Original DataFrames.

ia1, ia2 = df_staff1.align(df_staff2, join='right', axis=0)
print(ia1)
print(ia2)
  • Line [1] performs a right join on the DataFrames.
  • Line [2-3] outputs the results to the terminal.

Output

df_staff1

 ENameEIDAge
1Micah210032
2Barb210157

df_staff2

 ENameEIDAge
3Mark210275
4Jason210380
5Dakota210492

Summary

  • Both DataFrames have the 'EName', 'EID', and 'Age' labels in common.
  • Labels that appear in both DataFrames remain. Therefore 'Title' does not display.
  • No DataFrame values have changed. 

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.