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

How can we call dataframe functions like (mean, median, mode, min …) dynamically

fields = {‘rule1′:’min’,’rule2′:’max’}

For example:

for key, value in fields.items():
    Dataframe.{value}()

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

>Solution :

You can try getattr as below

fields = {
    'mean': df.mean,
    'median': df.median,
    'mode': df.mode,
    'min': df.min,
    'max': df.max
}

for key, value in fields.items():
        # Call the method using getattr()
        result = getattr(df, value)()
        print(f'The result of calling {key}() is: {result}')
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