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

Using .values.max() for arrays

I use the following code to get a row from a dataframe and then find the max value.

def find_max(a):
    return a.values.max()

row = df.iloc[0].astype(int)
max_value = find_max(a)

That works fine. However, if I pass an array like

ar = [1,2,3]
max_value = find_max(ar)

it doesn’t work and I receive AttributeError: 'list' object has no attribute 'values'. How can I use that function for both types?

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 :

def find_max(a):
    if isinstance(a, list):
        return max(a)
    elif isinstance(a, pd.DataFrame):
        return a.values.max()
    else:
        print("No max method found for the input type")
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