The Pandas DataFrame has several binary operator methods. When applied to a DataFrame, these methods combine two DataFrames and return a new DataFrame with the appropriate result.
This is Part 2 of the following series on Pandas DataFrame operators:
- Part 1: Pandas DataFrame Arithmetic Operators
- Part 2: Pandas DataFrame Reverse Methods
- Part 3: Pandas DataFrame Comparison Operators and Combine
Preparation
Before any data manipulation can occur, one (1) new library will require installation.
- The Pandas library enables access to/from a DataFrame.
To install this library, 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.
If the installation was successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required library.
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
DataFrame radd()
The radd()
, like the add()
method, takes a DataFrame and adds the parameter value to each element in the DataFrame.
These methods output the same result. An easy way to understand this is:
- The
add()
method uses: DataFrame1 + DataFrame2 - The
radd()
method uses: DataFrame2 + DataFrame1
This scenario reflects your interpretation of adding one element to another in a DataFrame.
The syntax for this method is as follows:
DataFrame.radd(other, axis='columns', level=None, fill_value=None)
Parameter | Description |
---|---|
other | This can be any single or multiple element data structure such as a list or list-like object. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
level | This parameter can be an integer or a label. This parameter is broadcast across a specified level and matches the index values on the MultiIndex level passed. |
fill_value | This parameter fills the NaN values before any computation occurs. If the data in both corresponding locations are missing, the result is missing. |
For this example, we have three levels and three sub-levels of Real Estate base commissions. It is the end of the year, and their Agency has decided to increase base commissions by one (1) across the board.
Code β Example 1
agents = {'junior': [0.5, 0.7, 0.8], 'middle': [1.2, None, 1.7], 'senior': [2.5, 1.9, None]} df = pd.DataFrame(agents) result = df.radd(1, fill_value=2.7) print(result)
- Line [1] creates a Dictionary called
agents
containing base commission rates for each level and sub-level. - Line [2] creates a DataFrame from this Dictionary and assigns this to
df
. - Line [3] does the following:
- Using
fill_value
assigns any None values to 2.7. - Updates the price changes after None values are replaced, and the
other
parameter is applied. - The data saves to the
result
variable.
- Using
- Line [4] outputs the result to the terminal.
Output
Formula Example: (middle) 1.2 + 1 = 2.2
junior | middle | senior | |
0 | 1.5 | 2.2 | 3.5 |
1 | 1.7 | 2.3 | 2.9 |
2 | 1.8 | 2.7 | 4.5 |
With the radd(n)
method, you can also apply different amounts to elements using a secondary DataFrame. This example contains a second Dictionary (craise
) with raises.
Code β Example 2
agents = {'junior': [0.5, 0.7, 0.8], 'middle': [1.2, 1.3, 1.7], 'senior': [2.5, 1.9, 3.5]} craise = {'junior': [1.1, 1.2, 1.3], 'middle': [2.4, 2.5, 2.6], 'senior': [3.7, 3.8, 3.9]} df1 = pd.DataFrame(agents) df2 = pd.DataFrame(craise) result = df1.radd(df2) print(result)
- Line [1] creates a Dictionary called
agents
containing base commission rates for each level and sub-level. - Line [2] creates a Dictionary called
craise
containing the raises to be applied. - Line [3-4] creates DataFrames from the Dictionaries listed above.
- Line [5] applies the craise DataFrame (
df2
) to the agents DataFrame (df1
). - Line [6] outputs the result to the terminal.
Output
Formula Example: (agents middle) 1.2 + (craise middle) 2.4 = 3.6
junior | middle | senior | |
0 | 1.6 | 3.6 | 6.2 |
1 | 1.9 | 3.8 | 5.7 |
2 | 2.1 | 4.3 | 7.4 |
Related Article: The Python Reverse Addition Method
DataFrame rsub()
The rsub()
method takes a DataFrame and subtracts the other
parameter value from each element in the DataFrame.
The syntax for this method is as follows:
DataFrame.rsub(other, axis='columns', level=None, fill_value=None)
Parameter | Description |
---|---|
other | This can be any single or multiple element data structure such as a list or list-like object. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
level | This parameter can be an integer or a label. This parameter is broadcast across a specified level and matches the index values on the MultiIndex level passed. |
fill_value | This parameter fills the NaN values before any computation occurs. If the data in both corresponding locations are missing, the result is missing. |
For this example, we have two Real Estate Agents. Our goal is to determine how many houses and condos Agent 1 sold over Agent 2 in San Diego’s three (3) real estate boroughs.
agent1 = pd.DataFrame({'homes-sold': [31, 55, 48], 'condos-sold': [13, 12, 14]}) agent2 = pd.DataFrame({'homes-sold': [1, 1, 7], 'condos-sold': [2, 5, 13]}) result = agent1.rsub(agent2) print(result)
- Line [1] creates a Dictionary called
agent1
containing the total number of houses and condos agent1 sold. - Line [2] creates a Dictionary called
agent2
containing the total number of houses and condosagent2
sold. - Line [3] subtracts these two DataFrames (element by element) and saves the output to the
result
variable. - Line [4] outputs the result to the terminal.
Output
Formula Example: (agent1 homes-sold) 31 β (agent2 homes-sold) = -30
homes-sold | condos-sold | |
0 | -30 | -11 |
1 | -54 | -7 |
2 | -41 | -1 |
To convert the above values from negative to positive, switch the DataFrames, or change line [4].
agent1 = pd.DataFrame({'homes-sold': [31, 55, 48], 'condos-sold': [13, 12, 14]}) agent2 = pd.DataFrame({'homes-sold': [1, 1, 7], 'condos-sold': [2, 5, 13]}) result = agent1.rsub(agent2) print(abs(result))
- Line [1] creates a Dictionary called
agent1
containing the total number of houses and condosagent1
sold. - Line [2] creates a Dictionary called
agent2
containing the total number of houses and condosagent2
sold. - Line [3] subtracts these two DataFrames (element by element) and saves the output to the
result
variable. - Line [4] converts the variable
result
to positive values and outputs to the terminal.
Related Article: The Python Reverse Subtraction Method
DataFrame rmul()
The rmul()
, like the mul()
method, takes a DataFrame and multiplies the parameter value to each element in the DataFrame.
These methods output the same result.
The syntax for this method is as follows:
DataFrame.rmul(other, axis='columns', level=None, fill_value=None)
Parameter | Description |
---|---|
other | This can be any single or multiple element data structure such as a list or list-like object. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
level | This parameter can be an integer or a label. This parameter is broadcast across a specified level and matches the index values on the MultiIndex level passed. |
fill_value | This parameter fills the NaN values before any computation occurs. If the data in both corresponding locations are missing, the result is missing. |
For this example, the base commission increases for all staff members of Rivers Clothing.
Code β DataFrame 1
df = pd.DataFrame({'Alice': [1.1], 'Bob': [1.8], 'Cindy': [1.6]}) result = df.rmul(2) print(result)
- Line [1] creates a Dictionary containing the staffβs base commission.
- Line [2] multiples the base commission by two (2) and saves it to the
result
variable. - Line [3] outputs the result to the terminal.
Output
Formula Example: (Alice) 1.1 * 2 = 2.2
Alice | Bob | Cindy | |
0 | 2.2 | 3.6 | 3.2 |
For this example, a new staff member joins Rivers Clothing. No base commission for the new hire is assigned.
Code β DataFrame 2
df = pd.DataFrame({'Alice': [1.1], 'Bob': [1.8], 'Cindy': [1.6], 'Micah': None}) result = df.rmul(2, fill_value=1.0) print(result)
- Line [1] creates a Dictionary containing the staffβs current base commission, including the new hire Micah.
- Line [2] multiples the current commission by two (2) after replacing
None
values with a fill value of 1.0. - Line [3] outputs the result to the terminal.
Output
Formula Example: (Alice) 1.1 * 2 = 2.2
Alice | Bob | Cindy | Micah | |
0 | 2.2 | 3.6 | 3.2 | 2.0 |
Related Article: The Python Reverse Multiplication Method
DataFrame rdiv()
The rdiv()
method takes a DataFrame and divides the parameter value against each element in the DataFrame.
The syntax for this method is as follows:
DataFrame.rdiv(other, axis='columns', level=None, fill_value=None)
Parameter | Description |
---|---|
other | This can be any single or multiple element data structure such as a list or list-like object. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
level | This parameter can be an integer or a label. This parameter is broadcast across a specified level and matches the index values on the MultiIndex level passed. |
fill_value | This parameter fills the NaN values before any computation occurs. If the data in both corresponding locations are missing, the result is missing. |
For this example, Rivers Clothing is having a sale on a few of its clothing items.
df = pd.DataFrame({'Tops': [15, 20, 25], 'Coats': [36, 88, 89], 'Pants': [21, 56, 94]}) result = df.rdiv(2).apply(lambda x:round(x,2)) print(result)
- Line [1] creates a Dictionary containing the items going on sale.
- Line [2] updates the price changes and rounds the result to two (2) decimal places.
- Line [3] outputs the result to the terminal.
Output
Formula Example: 2 / 15 = 0.13 (rounded to 2 decimal places)
Tops | Coats | Pants | |
0 | 0.13 | 0.06 | 0.10 |
1 | 0.10 | 0.02 | 0.04 |
2 | 0.08 | 0.02 | 0.02 |
Related Article: The Python Reverse Division Method
DataFrame rtruediv()
The rtruediv()
method takes a DataFrame and divides the parameter value against each element in the DataFrame.
The syntax for this method is as follows:
DataFrame.rtruediv(other, axis='columns', level=None, fill_value=None)
Parameter | Description |
---|---|
other | This can be any single or multiple element data structure such as a list or list-like object. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
level | This parameter can be an integer or a label. This parameter is broadcast across a specified level and matches the index values on the MultiIndex level passed. |
fill_value | This parameter fills the NaN values before any computation occurs. If the data in both corresponding locations are missing, the result is missing. |
For this example, Rivers Clothing is having a sale on all of its clothing items. Not all items have prices.
Code β Example 1
df = pd.DataFrame({'Tops': [15, 20, 25], 'Coats': [36, 88, 89], 'Pants': [21, 56, 94], 'Tanks': [11, 10, None], 'Sweats': [27, None, 35]}) index_ = ['Small', 'Medium', 'Large'] df.index = index_ result = df.rtruediv(other=2, fill_value=5).apply(lambda x:round(x,2)) print(result)
- Line [1] creates a Dictionary containing the items going on sale. Not all items have prices.
- Line [2-3] sets the index for the DataFrame.
- Line [4] does the following:
- Using
fill_value
assigns anyNone
values to 5. - Updates the price changes and rounds the result to two (2) decimal places after
None
values are applied. - The data saves to the
result
variable.
- Using
- Line [5] outputs the result to the terminal.
Output
Formula Example: 2 / 15 = 0.13 (rounded to 2 decimal places)
Tops | Coats | Pants | Tanks | Sweats | |
Small | 0.13 | 0.06 | 0.10 | 0.18 | 0.07 |
Medium | 0.10 | 0.02 | 0.04 | 0.20 | 0.40 |
Large | 0.08 | 0.02 | 0.02 | 0.40 | 0.06 |
This example assigns a different Price for each item across columns.
Code β Example 2
df = pd.DataFrame({'Tops': [15, 20, 25], 'Coats': [36, 88, 89], 'Pants': [21, 56, 94], 'Tanks': [11, 10, None], 'Sweats': [27, None, 35]}) index_ = ['Small', 'Medium', 'Large'] df.index = index_ result = df.rtruediv(other=[.1, .2, .3], axis=0, fill_value=.1).apply(lambda x:round(x,2)) print(result)
- Line [1] creates a Dictionary containing the items going on sale. Not all items have prices.
- Line [2-3] sets the index for the DataFrame.
- Line [4] does the following:
- Assigns a list of values to
other
to apply to the corresponding value in the DataFrame. - Axis is 0 (columns).
- Using
fill_value
assigns anyNone
values to .1. - The prices update after setting the
None
value(s) and applying the parameterother
. - Rounds the output to two (2) decimal places where applicable.
- These changes save to the
result
variable.
- Assigns a list of values to
- Line [5] outputs the result to the terminal.
Output
Formula Example: .1 / 15 = 0.01
Tops | Coats | Pants | Tanks | Sweats | |
Small | 0.01 | 0.0 | 0.0 | 1.01 | 0.00 |
Medium | 0.01 | 0.0 | 0.0 | 0.02 | 2.00 |
Large | 0.01 | 0.0 | 0.0 | 3.0 | 0.01 |
Related Article: The Python Reverse True Div Method
DataFrame rfloordiv()
The rfloordiv()
method takes a DataFrame and divides the parameter value to each element in the DataFrame. This method then rounds down (floor) the results.
The syntax for this method is as follows:
DataFrame.rfloordiv(other, axis='columns', level=None, fill_value=None
Parameter | Description |
---|---|
other | This can be any single or multiple element data structure such as a list or list-like object. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
level | This parameter can be an integer or a label. This parameter is broadcast across a specified level and matches the index values on the MultiIndex level passed. |
fill_value | This parameter fills the NaN values before any computation occurs. If the data in both corresponding locations are missing, the result is missing. |
This example uses the same DataFrame as above for Rivers Clothing.
df = pd.DataFrame({'Tops': [15, 20, 25], 'Coats': [36, 88, 89], 'Pants': [21, 56, 94], 'Tanks': [11, 10, None], 'Sweats': [27, None, 35]}) index_ = ['Small', 'Medium', 'Large'] df.index = index_ result = df.rfloordiv(2, fill_value=5) print(result)
- Line [1] creates a Dictionary containing the items going on sale. Not all items have prices.
- Line [2-3] sets the index for the DataFrame.
- Line [4] does the following:
- Round values to 2.
- Using
fill_value
assigns anyNone
values to 5. - The price changes are applied and rounded down (floor).
- These changes save to the
result
variable.
- Line [5] outputs the result to the terminal.
Output
Formula Example: 2 / 15 = .13333333 (rounds to 0)
Tops | Coats | Pants | Tanks | Sweats | |
Small | 0 | 0 | 0 | 0.0 | 0.0 |
Medium | 0 | 0 | 0 | 0.0 | 0.0 |
Large | 0 | 0 | 0 | 0.0 | 0.0 |
Related Article: The Python Reverse Floor Div Method
DataFrame rmod()
The rmod()
method determines the remainder by using the mod(n)
for each element in the DataFrame.
The syntax for this method is as follows:
DataFrame.rmod(other, axis='columns', level=None, fill_value=None)
Parameter | Description |
---|---|
other | This can be any single or multiple element data structure such as a list or list-like object. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
level | This parameter can be an integer or a label. This parameter is broadcast across a specified level and matches the index values on the MultiIndex level passed. |
fill_value | This parameter fills the NaN values before any computation occurs. If the data in both corresponding locations are missing, the result is missing. |
This example is a small representation of the available clothing items for Rivers Clothing.
df = pd.DataFrame({'Tops': [15, 20, 25], 'Coats': [36, 88, 89], 'Pants': [21, 56, 94]}) index_ = ['Small', 'Medium', 'Large'] df.index = index_ result = df.rmod(3) print(result)
- Line [1] creates a Dictionary containing a few items of Rivers Clothing,
- Line [2-3] sets the index for the DataFrame.
- Line [4] performs the mod operator on each element of the DataFrame and saves it to the
result
variable. - Line [5] outputs the result to the terminal.
Output
Formula Example: (tops medium) 3 % 20 = 3
Tops | Coats | Pants | |
Small | 3 | 3 | 3 |
Medium | 3 | 3 | 3 |
Large | 3 | 3 | 3 |
Related Article: The Python Reverse Modulo Method
DataFrame rpow()
The rpow()
method takes a DataFrame and performs the pow(n)
operator to each element in the DataFrame.
The syntax for this method is as follows:
DataFrame.rpow(other, axis='columns', level=None, fill_value=None)
Parameter | Description |
---|---|
other | This can be any single or multiple element data structure such as a list or list-like object. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
level | This parameter can be an integer or a label. This parameter is broadcast across a specified level and matches the index values on the MultiIndex level passed. |
fill_value | This parameter fills the NaN values before any computation occurs. If the data in both corresponding locations are missing, the result is missing. |
For this example, we have stock prices taken three times/day: Morning, Mid-Day, and Evening.
Code β Example 1
df1 = pd.DataFrame({'Stock-A': [9, 21.4, 20.4], 'Stock-B': [8.7, 8.7, 8.8], 'Stock-C': [21.3, 22.4, 26.5]}) df2 = pd.DataFrame({'Stock-A': [1, 2, 2], 'Stock-B': [3, 4, 5], 'Stock-C': [2, 3, 1]}) result = df1.rpow(df2).apply(lambda x:round(x,2)) print(result)
- Line [1] creates a Dictionary containing Stock Prices for three stocks, three times/day.
- Line [2] creates a Dictionary containing amounts to apply element-wise to DataFrame1 (
df1
) usingpow()
. - Line [3] applies the
pow()
method to each element ofdf1
and rounds the results to two (2) decimals places. - Line [4] outputs the result to the terminal.
Output
Formula Example: (Stock-A Mid-Day) 21.4 ** 2 = 457.96
Stock-A | Stock-B | Stock-C | |
0 | 1.00 | 14156.47 | 2.581897e+06 |
1 | 2767208.65 | 172950.54 | 4.869856e+10 |
2 | 1383604.33 | 1415585.28 | 1.000000e+00 |
Related Article: The Python Reverse Exponentiation Method
Related Articles:
- [Collection] 11 Python Cheat Sheets Every Python Coder Must Own
- [Python OOP Cheat Sheet] A Simple Overview of Object-Oriented Programming
- [Collection] 15 Mind-Blowing Machine Learning Cheat Sheets to Pin to Your Toilet Wall
- Your 8+ Free Python Cheat Sheet [Course]
- Python Beginner Cheat Sheet: 19 Keywords Every Coder Must Know
- Python Functions and Tricks Cheat Sheet
- Python Cheat Sheet: 14 Interview Questions
- Beautiful Pandas Cheat Sheets
- 10 Best NumPy Cheat Sheets
- Python List Methods Cheat Sheet [Instant PDF Download]
- [Cheat Sheet] 6 Pillar Machine Learning Algorithms