| by Arround The Web | No comments

Pandas New Column Based on Another Column

This article will illustrate two methods that you can use to create a new column based on the value of another column within a Pandas DataFrame.

Sample DataFrame.

In this tutorial, we will use an example DataFrame as shown below:

Using Pandas Apply Function

The first and most practical way of adding a new column based on another is using the Pandas apply function.

Suppose we want to return the rating of a movie as a percentage, we can do:

def percentage(x):

return (x / 10) * 100

df['%_rating'] = df.imdb_rating.apply(percentage)


In the example above, we define a function that takes the current rating, divided by 10, and multiplies it by 100.

We then create a new column called ‘%_rating’ and pass the user-defined function as a parameter to the apply() function.

This should return the new DataFrame as shown:

Using Element-Wise Operation

We can also create a new column using an element-wise operation instead of the apply function.

An example is illustrated below:

df['%_rating'] = (df['imdb_rating'] / 10) * 100


The code above should return:


This article illustrated two main methods of creating a new column based on a value from another column in Pandas.

Share Button

Source: linuxhint.com

Leave a Reply