­­Pandas add_prefix(), add_suffix(), align()

Rate this post

The Pandas DataFrame has several Re-indexing/Selection/Label Manipulations methods. When applied to a DataFrame, these methods evaluate, modify the elements and return the results.


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 add_prefix()

The add_prefix() method adds a specified string to the prefix (beginning) of a DataFrame/Series row or column label(s).

The syntax for this method is as follows:

DataFrame.add_prefix(prefix)
ParameterDescription
prefixIf working with a DataFrame, the prefix is pre-pended to the column label(s). If a Series, the row label(s) is the same.

For this example, the string 'Team_' is added as a prefix to the label(s) along the column axis.

df_teams = pd.DataFrame({'Bruins':   [4, 5, 9],
                         'Oilers':   [3, 6, 10],
                         'Leafs':    [2, 7, 11],
                         'Flames':   [1, 8, 12]},
                         index=['Ŵins', 'Losses', 'Ties'])

result = df_teams.add_prefix('Team_')
print(result)
  • Line [1] creates a DataFrame from a dictionary of lists, sets the index, and saves it to df_teams.
  • Line [2] uses the add_prefix() method to prefix the string at the appropriate spot. This output saves to the result variable.
  • Line [3] outputs the result to the terminal.

Output

 Team_Bruins Team_Oilers Team_Leafs Team_Flames
Wins4321
Losses5678
Ties9101112

DataFrame add_suffix()

The add_ suffix () method adds a specified string to the suffix (end) of a DataFrame/Series row or column label(s).

The syntax for this method is as follows:

DataFrame.add_suffix(suffix)
ParameterDescription
suffixIf working with a DataFrame, the suffix is pre-pended to the column label(s). If a Series, the row label(s) is the same.

For this example, the string '_Team ' is added as a suffix to the label(s) along the column axis.

df_teams = pd.DataFrame({'Bruins':   [4, 5, 9],
                         'Oilers':   [3, 6, 10],
                         'Leafs':    [2, 7, 11],
                         'Flames':   [1, 8, 12]},
                         index=['Ŵins', 'Losses', 'Ties'])

result = df_teams.add_prefix('_Team')
print(result)
  • Line [1] creates a DataFrame from a dictionary of lists, sets the index, and saves it to df_teams.
  • Line [2] uses the add_suffix() method to suffix the string at the appropriate spot. This output saves to the result variable.
  • Line [3] outputs the result to the terminal.

Output

 Bruins_Team Oilers_Team Leafs_Team Flames_Team
Wins4321
Losses5678
Ties9101112

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. 

Further Learning Resources

This is Part 6 of the DataFrame method series.

  • Part 1 focuses on the DataFrame methods abs(), all(), any(), clip(), corr(), and corrwith().
  • Part 2 focuses on the DataFrame methods count(), cov(), cummax(), cummin(), cumprod(), cumsum().
  • Part 3 focuses on the DataFrame methods describe(), diff(), eval(), kurtosis().
  • Part 4 focuses on the DataFrame methods mad(), min(), max(), mean(), median(), and mode().
  • Part 5 focuses on the DataFrame methods pct_change(), quantile(), rank(), round(), prod(), and product().
  • Part 6 focuses on the DataFrame methods add_prefix(), add_suffix(), and align().
  • Part 7 focuses on the DataFrame methods at_time(), between_time(), drop(), drop_duplicates() and duplicated().
  • Part 8 focuses on the DataFrame methods equals(), filter(), first(), last(), head(), and tail()
  • Part 9 focuses on the DataFrame methods equals(), filter(), first(), last(), head(), and tail()
  • Part 10 focuses on the DataFrame methods reset_index(), sample(), set_axis(), set_index(), take(), and truncate()
  • Part 11 focuses on the DataFrame methods backfill(), bfill(), fillna(), dropna(), and interpolate()
  • Part 12 focuses on the DataFrame methods isna(), isnull(), notna(), notnull(), pad() and replace()
  • Part 13 focuses on the DataFrame methods drop_level(), pivot(), pivot_table(), reorder_levels(), sort_values() and sort_index()
  • Part 14 focuses on the DataFrame methods nlargest(), nsmallest(), swap_level(), stack(), unstack() and swap_axes()
  • Part 15 focuses on the DataFrame methods melt(), explode(), squeeze(), to_xarray(), t() and transpose()
  • Part 16 focuses on the DataFrame methods append(), assign(), compare(), join(), merge() and update()
  • Part 17 focuses on the DataFrame methods asfreq(), asof(), shift(), slice_shift(), tshift(), first_valid_index(), and last_valid_index()
  • Part 18 focuses on the DataFrame methods resample(), to_period(), to_timestamp(), tz_localize(), and tz_convert()
  • Part 19 focuses on the visualization aspect of DataFrames and Series via plotting, such as plot(), and plot.area().
  • Part 20 focuses on continuing the visualization aspect of DataFrames and Series via plotting such as hexbin, hist, pie, and scatter plots.
  • Part 21 focuses on the serialization and conversion methods from_dict(), to_dict(), from_records(), to_records(), to_json(), and to_pickles().
  • Part 22 focuses on the serialization and conversion methods to_clipboard(), to_html(), to_sql(), to_csv(), and to_excel().
  • Part 23 focuses on the serialization and conversion methods to_markdown(), to_stata(), to_hdf(), to_latex(), to_xml().
  • Part 24 focuses on the serialization and conversion methods to_parquet(), to_feather(), to_string(), Styler.
  • Part 25 focuses on the serialization and conversion methods to_bgq() and to_coo().

Also, have a look at the Pandas DataFrame methods cheat sheet!