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

passing operators to pandas dataframe in function:

Say I want to iterate say I have pandas dataframe:

import pandas as pd
tmp = pd.DataFrame([(2,2),(0,4),(1,3)],columns= ['A','B'])

and I want to write something of the following kind:

def f(operator,column)

s.t

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

f('mean','A')

will return:

tmp['A'].mean()

>Solution :

As the complement of @mozway answer, you can use agg:

>>> tmp.agg({'A': 'mean', 'B': 'max'})
A    1.0
B    4.0
dtype: float64

Take a look to here: check the last lines of code. agg already use getattr.

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