Passing multiple arguments to a function using Pandas apply and expand a list returned by a function to multiple columns via Apply function

Pandas – Tricks Part 1 (Data Analysis with python)

Passing multiple arguments to a function using Pandas apply and expand a list returned by a function to multiple columns via Apply function. Part of Data analysis with Python.

Pandas is an extremely useful tool for Data Analysis. So, lets dive straight into some tricks that will make your life simpler using Pandas apply function. In this blog post , we will learn about how to unleash the power of pandas apply function. Following topics covered.

1. Apply operation .

2. How to pass multiple arguments to function using pandas using args parameter?

3. How to create multiple columns using pandas apply using result_type parameter?

A. Data analysis with Python (Tip #1).

Create a Data frame(Table) using random data.

 ab
010.023164
120.471015
230.812351
340.898299
450.001414

B. Data analysis with Python (Tip #2).

Perform a computational operation on the existing column.

  • Create a new column “c” which adds 1 to every element in column “a”.
  • Apply function applies a function to each and every element of column “a”
  • Instead of defining a new function, make use of anonymous function lambda which adds 1 to every element of column “a”.

 abc
010.0231642
120.4710153
230.8123514
340.8982995
450.0014146

C. Data analysis with Python (Tip #3).

Pass multiple arguments to a function using apply.

  • Generally, the column on which some operation has to be done is passed as an argument to the functions.
  • In case some extra arguments has to be passed , use the following “args” parameter that is present within apply function and pass a list of arguments.
  • For e.g, in the below code, if values of column “a” is below 3 , then argument 1 i.e “Good” is concatenated with column “a” value or else argument 2 i.e “Bad” is concatenated with column “a” value.
 abcConcat
010.61393121_Good
120.39820532_Good
230.28806243_Bad
340.92439754_Bad
450.45426065_Bad

D. Data analysis with Python (Tip #4).

Expand a list returned by a function to multiple columns using Apply

  • When a function returns multiple values , return those values as a list and create multiple columns out of it using apply.
  • For example, in the below code, multiple_args_return_function, takes the column “a” and depending on the values of column “a”, it returns 3 different values “Hey”,”Good”,”Girl” or “Hey”,”Good”,”Boy”.
  • To do this , the parameter “result_type= ‘expand’” is used , which converts a list of multiple values returned into multiple columns .
 abcConcatResult1Result2Result3
010.02316421_GoodHeyGoodGirl
120.47101532_GoodHeyGoodGirl
230.81235143_GoodHeyGoodBoy
340.89829954_GoodHeyGoodBoy
450.00141465_GoodHeyGoodBoy

If you find this blogpost helpful, please hit the share button and feel free to share it with your friends .

Until then goodbye

Leave a comment

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