Python DataFrame Reverse Methods – Part 2

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:


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)
ParameterDescription
otherThis can be any single or multiple element data structure such as a list or list-like object.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
levelThis 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_valueThis 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.
  • Line [4] outputs the result to the terminal.

Output

Formula Example: (middle) 1.2 + 1 = 2.2

 juniormiddlesenior
01.52.23.5
11.72.32.9
21.82.74.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

 juniormiddlesenior
01.63.66.2
11.93.85.7
22.14.37.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)
ParameterDescription
otherThis can be any single or multiple element data structure such as a list or list-like object.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
levelThis 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_valueThis 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 condos agent2 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-soldcondos-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 condos agent1 sold.
  • Line [2] creates a Dictionary called agent2 containing the total number of houses and condos agent2 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)
ParameterDescription
otherThis can be any single or multiple element data structure such as a list or list-like object.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
levelThis 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_valueThis 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

 AliceBobCindy
02.23.63.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

 AliceBobCindyMicah
02.23.63.22.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)
ParameterDescription
otherThis can be any single or multiple element data structure such as a list or list-like object.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
levelThis 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_valueThis 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)

 TopsCoatsPants
00.13  0.06  0.10
10.10  0.02  0.04
20.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)
ParameterDescription
otherThis can be any single or multiple element data structure such as a list or list-like object.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
levelThis 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_valueThis 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 any None 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.
  • Line [5] outputs the result to the terminal.

Output

Formula Example: 2 / 15 = 0.13 (rounded to 2 decimal places)

 TopsCoatsPantsTanksSweats
Small0.130.060.100.180.07
Medium0.100.020.040.200.40
Large0.080.020.020.400.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 any None values to .1.
    • The prices update after setting the None value(s) and applying the parameter other.
    • Rounds the output to two (2) decimal places where applicable.
    • These changes save to the result variable.
  • Line [5] outputs the result to the terminal.

Output

Formula Example: .1 / 15 = 0.01

 TopsCoatsPantsTanksSweats
Small0.010.00.01.010.00
Medium0.010.00.00.022.00
Large0.010.00.03.00.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
ParameterDescription
otherThis can be any single or multiple element data structure such as a list or list-like object.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
levelThis 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_valueThis 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 any None 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)

 TopsCoatsPantsTanksSweats
Small0000.00.0
Medium0000.00.0
Large0000.00.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)
ParameterDescription
otherThis can be any single or multiple element data structure such as a list or list-like object.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
levelThis 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_valueThis 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

 TopsCoatsPants
Small333
Medium333
Large333

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)
ParameterDescription
otherThis can be any single or multiple element data structure such as a list or list-like object.
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
levelThis 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_valueThis 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) using pow().
  • Line [3] applies the pow() method to each element of df1 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-AStock-BStock-C
01.00   14156.472.581897e+06
12767208.65  172950.54 4.869856e+10
21383604.331415585.281.000000e+00

Related Article: The Python Reverse Exponentiation Method