Python

Divide Two Columns Pandas

Pandas is a wonderful Python tool that lets you modify DataFrames and datasets. It has several handy data manipulation functions. There are occasions when you need to split two columns in pandas. You will learn how to divide two columns in pandas using several ways throughout this lesson.

In this post, you will learn how to divide two columns in Pandas using several approaches. Please note that we are using the Spyder IDE to implement all of the examples. To gain a better understanding, make sure to use all of the applications.

What Is a Pandas DataFrame?

The Pandas DataFrame is defined as a structure for storing two-dimensional data and the accompanying labels. DataFrames are commonly used in disciplines that deal with vast amounts of data, such as data science, scientific machine learning, scientific computing, and others.

DataFrames are similar to SQL tables, Excel and Calc spreadsheets. DataFrames are frequently faster, simpler to use, and far more powerful than tables or spreadsheets since they are an integral part of the Python and NumPy ecosystems.

Before moving on to the next section, we will go through some programming examples of how to divide two columns. To begin, we will need to generate a sample DataFrame.

We will begin by generating a small DataFrame with some data so you can follow along with the examples.

The Pandas module is imported, and two columns with different values are declared, as shown in the code below. Then, we used the pandas.dataframe function to build the DataFrame and print the output.

import pandas

First_Column = [65, 44, 102, 334]

Second_Column = [8,12,34,33]

result = pandas.DataFrame (dict(First_Column = First_Column, Second_Column = Second_Column))

print(result.head())

The DataFrame that was built is displayed here.

Now, let’s look at some specific examples to see how you can divide two columns with Python’s Pandas package.

Example 1: 

The simple division (/) operator is the first way to divide two columns. You will split the First Column with the other columns here. This is the simplest method of dividing two columns in Pandas. We will import Pandas and take at least two columns while declaring the variables. The division value will be saved in the division variable when dividing columns with division operators(/).

Execute the lines of code listed below. As you can see in the code below, we first produce data and then use the pd.DataFrame() method to transform it into a DataFrame. Finally, we divide d_frame [“First_Column”] by d_frame[“Second_Column”] and assign the result column to the result.

import pandas

values = {"First_Column":[65, 44, 102, 334],"Second_Column":[8,12,34,33]}

d_frame = pandas.DataFrame(values)

d_frame["result"] = d_frame["First_Column"]/d_frame["Second_Column"]

print(d_frame)

You will get the following output if you run the above reference code. The numbers obtained by dividing ‘First_Column’ by ‘Second_Column’ are stored in the third column named ‘result.’

Example 2: 

The div() technique is the second way to divide two columns. It separates the columns into sections based on the elements they include. It accepts a series, scalar value, or DataFrame as an argument for division with the axis. When the axis is zero, division takes place row by row when the axis is set to one, division takes place column by column.

The div() method finds the floating division of a DataFrame and other elements in Python. This function is identical to dataframe/other, except it has the added capability of handling missing values in one of the incoming data sets.

Run the lines of the following code. We are dividing First_Column by the value of Second_Column in the code below, bypassing the d_frame[“Second_Column”] values as an argument. The axis is set to 0 by default.

import pandas

values = {"First_Column":[456,332,125,202,123],"Second_Column":[8,10,20,14,40]}

d_frame = pandas.DataFrame(values)

d_frame["result"] = d_frame["First_Column"].div(d_frame["Second_Column"].values)

print(d_frame)

The following image is the output of the preceding code:

Example 3:

In this example, we will conditionally divide two columns. Let’s say you wish to separate two columns into two groups based on a single condition. We want to divide First Column by Second Column only when First Column values are greater than 300, for example. You must use the np.where() method.

The numpy.where() function chooses the elements from a NumPy array that depends on specific criteria.

Not only that, but if the condition is met, we can conduct some operations on those elements. This function takes a NumPy-like array as an argument. It returns a new NumPy array, which is a NumPy-like array of Boolean values, after filtering according to criteria.

It accepts three different types of parameters. The condition comes first, followed by the outcomes, and finally, the value when the condition is not met. We are going to use the NaN value in this scenario.

Execute the following piece of code. We have imported the pandas and NumPy modules, which are essential for this application to run. Following that, we built the data for the First_Column and Second_Column columns. The First_Column has 456, 332, 125, 202, 123 values, whereas the Second_Column contains 8, 10, 20, 14, and 40 values. After that, the DataFrame is constructed using the pandas.dataframe function. Finally, the numpy.where method is used to separate two columns using the given data and a certain criterion. All of the stages may be found in the code below.

import pandas

import numpy

values = {"First_Column":[456,332,125,202,123],"Second_Column":[8,10,20,14,40]}

d_frame = pandas.DataFrame(values)

d_frame["result"] = numpy.where(d_frame["First_Column"]>300,

d_frame["First_Column"]/d_frame["Second_Column"],numpy.nan)

print(d_frame)

If we divide two columns using Python’s np.where function, we get the following result.

Conclusion

This article covered how to divide two columns in Python in this tutorial. To do this, we used the division (/) operator, the DataFrame.div() method, and the np.where() function. The Python modules Pandas and NumPy were discussed, which we used to execute the mentioned scripts. Furthermore, we have solved problems using these methods on the DataFrame and have a good understanding of the method. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content