Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Apply a custom function to each column of a pandas dataframe

Let say I have below dataframe

import pandas as pd
Dat = pd.DataFrame({'a' : [1,2], 'b' : [3,4]})

def MyFunc(x, y) :
  return x + y ** (.5)

Now I want to apply above custom function to elements of each of the columns of Dat.

Is there any function/method to achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have gone through different suggestions over internet, however all of them talk mainly application of standard python function e.g. sum()

Any pointer will be very helpful.

>Solution :

You don’t need a function, you can use directly:

Dat.iloc[0]+Dat.iloc[1]**.5

If really you need the function:

MyFunc(Dat.iloc[0], Dat.iloc[1])

Or:

tmp = Dat.T

MyFunc(tmp[0], tmp[1])

Output:

a    2.414214
b    5.000000
dtype: float64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading