In Python, Pandas is a wonderful tool defined as a structure to modify datasets and Dataframe. It has multiple data manipulation methods that can perform different operations, such as sorting the two-dimensional data in Python, splitting columns data and many more.
This post will explain the methods of dividing two columns Pandas in Python.
How to Divide Two Columns Pandas in Python?
To divide two columns of pandas in Python, the following techniques are used:
- Built-in Division (/) Operator
Method 1: Divide Two Columns Pandas by Utilizing “/” Operator in Python
The “/” is the division operator, which is the first easiest way of dividing two columns in Pandas. Using this operator, you can split one column with the others in the example below.
First, import the “pandas” library as “pd”:
Now, declare a variable that takes at least the values of two columns. For instance, we have declared the “col_values” variable, “Column1” and “Column2” two columns:
Next, call the “pd.DataFrame()” method to convert the declared variable into a dataframe:
Finally, use the “/” operator to divide the above-declared “Column1” and “Column2” into two columns. Then, assign to the “Result Column”:
Call the “print()” function to view the results:
According to the below-provided output, the “Column1” and “Column2” have been divided successfully, and the result is stored in the other column named “Result Column”:
Method 2: Divide Two Columns Pandas Built-in “div()” Function in Python
You can also utilize the Pandas built-in “div()” method to divide the values of two columns in Python. It returns the floating division of dataframes, and element-wise. To do so, check out the below-stated example.
Call the “div()” method to divide the previously declared “Column1” and “Column2” into two columns. Then, assign into the result column named “Result Column”:
Method 3: Divide Two Columns Pandas Conditionally by Utilizing “np.where” Function in Python
Sometimes users want to divide the column into their particular condition in Python, the “np.where()” method can be used for this purpose that accepts three arguments, likewise first one is the desired condition, second is the result, and last one is the particular value where the condition is not met.
First, import the pandas library as “pd” and the numpy library as “np”:
import numpy as np
Then, call the “np.where()” function that takes the desired condition, result, and value where the provided condition is not true. For instance, we used the “NaN” value and passed it to the result column named “Result Column”. Lastly, call the “print()” function to get the result:
As you can see that the provided two columns have been divided successfully:
That’s all! We have illustrated different methods to divide two columns Pandas.
To divide two columns Pandas, the built-in “/” operator, “div()” method, and “np.where()” method are used. The “/” operator is the division operator, which is the simplest way of columns in Pandas. The “div()” method returns the floating division of the dataframe and element-wise, and the “np.where()” method accepts the condition, result, and particular value where the specified condition is not met. This post demonstrated the methods of dividing two columns Pandas.