How to divide two Columns in Pandas : Various Methods

Divide two Columns in Pandas

Pandas is a great Python module that allows you to manipulate the dataframe or your dataset. There are many functions in it that efficiently do manipulation. There is a time when you need to divide two columns in pandas. In this entire tutorial, you will how to divide two columns in pandas using different methods.

Methods to divide two columns in Pandas

In this section, you will know all the methods to divide two columns in pandas. Please note that I am implementing all the examples on Jupyter Notebook. Make sure to do it for better understanding.

Method 1: Using a simple division operator

The first method you can use to divide two columns is the simple division (/) operator. Here you will divide column1 with other columns.

Execute the below lines of code.

import pandas as pd
data = {"col1":[100,200,300,400,500],"col2":[10,20,30,40,50]}
df= pd.DataFrame(data)
df["result"] = df["col1"]/df["col2"]
print(df)

You can see in the above code I am first creating data and converting it to dataframe using pd.DataFrame() method. Lastly, I am dividing df[“col1”] with df[“col2”] and assigning it to the result column.

When you will run the code you will get the following output.

Output

Dividing two column using division operator
Dividing two columns using the division operator

Method 2: Pandas divide two columns using div() function

The second method to divide two columns is using the div() method. It divides the columns elementwise. It accepts a scalar value, series, or dataframe as an argument for dividing with the axis. If the axis is 0 the division is done row-wise and if the axis is 1 then the division is done column-wise.

Execute the below lines of code.

import pandas as pd
data = {"col1":[100,200,300,400,500],"col2":[10,20,30,40,50]}
df= pd.DataFrame(data)
df["result"] = df["col1"].div(df["col2"].values)
print(df)

In the above, you can see I am dividing col1 with the value of col2 bypassing the df[“col2”] .values as an argument. By default, the axis is 0.

Output

Dividing two column using division operator
Dividing two column using div() function

 

Method 3: Dividing two columns conditionally using np.where()

Suppose you want to divide two columns based on one condition. For example, I want to divide col1 with col2 only when col1 values are more than 300. To do so you have to use np.where() method for that.

It accepts three arguments. One is the condition, the second is the results and the third is the value where the condition is not met. In our case, I am using the NaN value.

Execute the below lines of code.

import pandas as pd
import numpy as np
data = {"col1":[100,200,300,400,500],"col2":[10,20,30,40,50]}
df= pd.DataFrame(data)
df["result"] = np.where(df["col1"]>300, df["col1"]/df["col2"],np.nan)
print(df)

Output

Divide two columns conditionally
Divide two columns conditionally

Conclusion

Pandas python module can do fast manipulation on any dataframe. These are the method to divide two columns in dataframe. You can use any of them. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Source:

Pandas div Documentation

Frequently Asked Questions:

 

1. How to divide DataFrame columns in Pandas using any specific values?

This is really simple as we have performed in our above solution 1, we can simply use the value in the place of another column. Here is a syntax example for this.

import pandas as pd
value=10
data = {"col1":[100,200,300,400,500],"col2":[10,20,30,40,50]}
df= pd.DataFrame(data)
df["result"] = df["col1"]/value
print(df)

Here you can change the value as per your requirement.

 

2. How to divide multiple columns by another column in Pandas?

we can simply use any of the above methods by passing multiple columns altogether. Here is an example of this –

import pandas as pd
data = {"col1":[100,200,300,400,500],"col2":[10,20,30,40,50]}
df= pd.DataFrame(data)
df["result"] = df[['col1','col3']].div(df["col2"].values)
print(df)

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner