HomeLinuxDivide Two Columns Pandas

Divide Two Columns Pandas


In Python, Pandas is a superb software outlined as a construction to switch datasets and Dataframe. It has a number of information manipulation strategies that may carry out completely different operations, akin to sorting the two-dimensional information in Python, splitting columns information and plenty of extra.

This put up will clarify the strategies of dividing two columns Pandas in Python.

The best way to Divide Two Columns Pandas in Python?

To divide two columns of pandas in Python, the next strategies are used:

Technique 1: Divide Two Columns Pandas by Using “/” Operator in Python

The “/” is the division operator, which is the primary simplest way of dividing two columns in Pandas. Utilizing this operator, you possibly can break up one column with the others within the instance under.

Instance

First, import the “pandas” library as “pd”:

Now, declare a variable that takes at the very least the values of two columns. For example, we’ve declared the “col_values” variable, “Column1” and “Column2” two columns:

col_values = {“Column1”:[20, 40, 60, 80, 100],“Column2”:[10, 30, 50, 70, 90]}

Subsequent, name the “pd.DataFrame()” methodology to transform the declared variable right into a dataframe:

data_f= pd.Dataframe(col_values)

Lastly, use the “/” operator to divide the above-declared “Column1” and “Column2” into two columns. Then, assign to the “End result Column”:

data_f[“Result Column”] = data_f[“Column1”] / data_f[“Column2”]

Name the “print()” perform to view the outcomes:

Based on the below-provided output, the “Column1” and “Column2” have been divided efficiently, and the result’s saved within the different column named “End result Column”:

Technique 2: Divide Two Columns Pandas Constructed-in “div()” Perform in Python

You may as well make the most of the Pandas built-in “div()” methodology to divide the values of two columns in Python. It returns the floating division of dataframes, and element-wise. To take action, take a look at the below-stated instance.

Instance

Name the “div()” methodology to divide the beforehand declared “Column1” and “Column2” into two columns. Then, assign into the end result column named “End result Column”:

data_f[“Result Column”] = data_f[“Column1”].div(data_f[“Column2”])

Output

Technique 3: Divide Two Columns Pandas Conditionally by Using “np.the place” Perform in Python

Typically customers need to divide the column into their specific situation in Python, the “np.the place()” methodology can be utilized for this function that accepts three arguments, likewise first one is the specified situation, second is the end result, and final one is the actual worth the place the situation is just not met.

Instance

First, import the pandas library as “pd” and the numpy library as “np”:

import pandas as pd

import numpy as np

Then, name the “np.the place()” perform that takes the specified situation, end result, and worth the place the supplied situation is just not true. For example, we used the “NaN” worth and handed it to the end result column named “End result Column”. Lastly, name the “print()” perform to get the end result:

data_f[“Result Column”] = np.the place(data_f[“Column1”]>40, data_f[“Column1”]/data_f[“Column2”],np.nan)

print(data_f)

As you possibly can see that the supplied two columns have been divided efficiently:

That’s all! We’ve got illustrated completely different strategies to divide two columns Pandas.

Conclusion

To divide two columns Pandas, the built-in “/” operator, “div()” methodology, and “np.the place()” methodology are used. The “/” operator is the division operator, which is the best means of columns in Pandas. The “div()” methodology returns the floating division of the dataframe and element-wise, and the “np.the place()” methodology accepts the situation, end result, and specific worth the place the desired situation is just not met. This put up demonstrated the strategies of dividing two columns Pandas.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments