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)
Parameter | Description |
---|---|
prefix | If 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 theresult
variable. - Line [3] outputs the result to the terminal.
Output
Team_Bruins | Team_Oilers | Team_Leafs | Team_Flames | |
Wins | 4 | 3 | 2 | 1 |
Losses | 5 | 6 | 7 | 8 |
Ties | 9 | 10 | 11 | 12 |
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)
Parameter | Description |
---|---|
suffix | If 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 theresult
variable. - Line [3] outputs the result to the terminal.
Output
Bruins_Team | Oilers_Team | Leafs_Team | Flames_Team | |
Wins | 4 | 3 | 2 | 1 |
Losses | 5 | 6 | 7 | 8 |
Ties | 9 | 10 | 11 | 12 |
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)
Parameter | Description |
---|---|
other | This parameter can be a DataFrame/Series. |
join | Joins on the: outer , inner , left , or right . |
axis | Can be aligned: index (0), columns (1), or both, which is None . |
level | Broadcast over a level. Level matches the index value(s) on a multi-index level. |
copy | If True , this parameter returns a copy of the original DataFrame/Series. If False , no re-indexing is needed. The original DataFrame/Series returns. |
fill_value | If there is missing data in the DataFrame/Series, the missing data, by default, is set as NaN , or any other valid value. |
method | This parameter can be: backfill , bfill , pad , ffill , or None by default. |
limit | If this parameter contains data, it will fill according to the method selected for x number of NaN values. |
fill_axis | This 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
anddf_staff2
. - Line [3] outputs the results to the terminal.
Output
df_staff1
EName | EID | Title | Age | |
1 | Micah | 2100 | Analyst | 32 |
2 | Barb | 2101 | Coder | 57 |
df_staff2
EName | EID | Age | |
3 | Mark | 2102 | 75 |
4 | Jason | 2103 | 80 |
5 | Dakota | 2104 | 92 |
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
EName | EID | Age | |
1 | Micah | 2100 | 32 |
2 | Barb | 2101 | 57 |
df_staff2
EName | EID | Age | |
3 | Mark | 2102 | 75 |
4 | Jason | 2103 | 80 |
5 | Dakota | 2104 | 92 |
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
Age | EID | EName | Title | |
1 | 32 | 2100 | Micah | Analyst |
2 | 57 | 2101 | Barb | Coder |
df_staff2
Age | EID | EName | Title | |
3 | 75 | 2102 | Mark | NaN |
4 | 80 | 2103 | Jason | NaN |
5 | 92 | 2104 | Dakota | NaN |
Summary
- The original DataFrame
df_staff2
does not contain'Title'
. This label displaysNaN
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
EName | EID | Title | Age | |
1 | Micah | 2100 | Analyst | 32 |
2 | Barb | 2101 | Coder | 57 |
df_staff2
EName | EID | Title | Age | |
3 | Mark | 2102 | NaN | 75 |
4 | Jason | 2103 | NaN | 80 |
5 | Dakota | 2105 | NaN |
Summary
- The original DataFrame
df_staff2
does not contain'Title'
. This label displaysNaN
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
EName | EID | Age | |
1 | Micah | 2100 | 32 |
2 | Barb | 2101 | 57 |
df_staff2
EName | EID | Age | |
3 | Mark | 2102 | 75 |
4 | Jason | 2103 | 80 |
5 | Dakota | 2104 | 92 |
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.
Also, have a look at the Pandas DataFrame methods cheat sheet!