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:
return (x / 10) * 100
df['%_rating'] = df.imdb_rating.apply(percentage)
df
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
The code above should return:
Conclusion
This article illustrated two main methods of creating a new column based on a value from another column in Pandas.