Share on facebook
Share on twitter
Share on linkedin
Share on pinterest

Pandas Math Functions

In this article we’ll learn some of the commonly used math functions in Pandas. Let’s get started.

The abs() function:

The first one we are going to see is the abs() function. This function returns the absolute values for the elements in the data frame.

There are few negative values. Let’s apply the abs function.

The abs() function can also be applied to complex numbers. In the case of complex numbers the absolute value is calculated as \sqrt{a^{2}+b^{2}}

Let’s create a new column B and populate it with complex numbers.

After applying the abs function the results are as follows

The clip() function:

The clip takes two values lower limit and upper limit. If the values in the data frame exceeds this value then it will be clipped to the upper limit. If it is lower than the lower limit then it will be increased to the lower limit.

The count() function:

This function will return the number of non null values in the data frame for each column. Let’s apply the count function on the data frame.

The following is our data frame

Let’s apply count function on this data frame.

Since there are no null values in the data frame the result is ten which is the number of observations. Let’s introduce a null value in the column B and again apply count function.

Now, if we apply the count function we’ll get the following results

By specifying the axis=1 we can get the number of non null values for each row.

The min, med, mean, max:

These functions are straight forward.

One thing worth mentioning is that by default the null values are not included in the calculation. If you want to include them you can set the parameter skipna to False.

The rank() function:

The rank() function returns the rank of the values in the data frame. Consider the following data frame.

The result of applying rank on the above data frame is as follows

By default it ranks the values in ascending order. If you don’t want to rank the values in the ascending order you can set the parameter ascending to False.

Another important argument worth mentioning is the method. It helps to decide what to do in case of a tie. There are various options like average, min, max, first. Let’s take a look at each of them.

Consider the following data frame.

Average will use the average rank of the group and apply to all items with the same rank.

Let’s see how these ranks are assigned.

The smallest element here is 0. Since there are three of them they will get the ranks 1,2 and 3.

Now we need to calculate the average of these ranks i.e., (3 + 2 + 1) / 3 = 2. So, the rank 2 will be assigned to all the zeros.

The next smallest element is 1. Since there are already three elements which are smaller than 1 we’ll assign a rank of 4 and 5 for the two ones. Again we’ll apply the average to find the rank of 1. The average of 4 and 5 is 4.5

So, the rank 4.5 will be assigned to 1.

The last element 2 will get a rank of 6.

Now let’s see how the ‘min’ will assign the ranks.

The min will assign lowest rank to all items.

Similarly the ‘max’ will assign highest rank to all items.

In ‘first’ the rank to the items will be assigned in the order they appear.

Summary:

In this article we learned some of the commonly used math functions in Pandas. 

We discussed a total of five functions. They are:

abs(): returns the absolute values for the elements in the data frame.

clip(): if the values in the data frame exceeds the upper or lower limit it will be clipped.

count(): returns the number of non null values in the data frame.

min, max, med, mode: returns the min, max, med and mode for each column in the data frame.

rank(): return the rank of the elements in the data frame.

Love What you Read. Subscribe to our Newsletter.

Stay up to date! We’ll send the content straight to your inbox, once a week. We promise not to spam you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe Now! We'll keep you updated.