Divide two columns in Pandas DataFrame

Divide two columns in Pandas DataFrame

When working with a data science or machine learning project it is common to use a Pandas DataFrame to store the data, however when it comes to feature engineering it can be confusing to know what options are available for arithmetic operations of columns or rows

Stephen Allwright
Stephen Allwright

Divide two columns in Pandas

When working with a data science or machine learning project it is common to use a Pandas DataFrame to store the data, however when it comes to feature engineering it can be confusing to know what options are available for arithmetic operations of columns or rows. This is often down to the fact that Pandas has some built in methods, but there are also more Pythonic methods of arithmetic as well. In this post I will go through the different options one has for dividing columns in a Pandas DataFrame.

Divide two columns using built-in Python division operation

The simplest approach for dividing two columns is to use the built in division operator in Python. This provides little in the way of arguments or adjustments, but is the best approach to take when you want to simply divide two columns element-wise

import pandas as pd

df = pd.DataFrame({'tables': [10, 20, 30],
                   'chairs': [50, 40, 60]})
                   
df["chairs_per_table"] = df["chairs"] / df["tables"]

Divide two columns using Pandas division method

The method that offers the most flexibility and options is the Pandas division method. It allows us to divide element-wise on either axis and also at multiple levels.

Here is an example showing how to divide two columns in a Pandas DataFrame element-wise using the Pandas division method:

import pandas as pd 

df = pd.DataFrame({'tables': [10, 20, 30],
                   'chairs': [50, 40, 60]})
                   
df["chairs_per_table"] = df["chairs"].div(df["tables"],axis=1)

Pandas GroupBy aggregate functions
Remove outliers
Scale columns

References

Pandas div documentation
Numpy divide documentation

Pandas

Stephen Allwright Twitter

I'm a Data Scientist currently working for Oda, an online grocery retailer, in Oslo, Norway. These posts are my way of sharing some of the tips and tricks I've picked up along the way.