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

ValueError: Use a.empty, a.bool(), a.item(), a.any() or a.all() & AttributeError: 'bool' object has no attribute 'bool'

I would like to create a function which could convert some concentrations into nanomolar concentration. So I wrote this function:

def convert_molars(df, field_values, field_units):

    if (df[field_units].str.contains('uM')): # 1 uM = 1000 nM
        df[field_values] *= 1000
    
    elif (df[field_units].str.contains('M')): # 1 M = 1000000000 nM
        df[field_values] *= 1000000000
    
    else: # 1 mM = 1000000 nM
        df[field_values] *= 1000000
    
    return df

And I started it like this:

standard_units = convert_molars(IC50_nonan_units, 'Standard Value', 'Standard Units')
standard_units.to_csv("standard_units.csv")
standard_units.head()

But I got this error:

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

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried one more option like this:

def convert_molars_bool(df, field_values, field_units):

    if (df[field_units].str == 'uM').bool(): # 1 uM = 1000 nM
        df[field_values] *= 1000
    
    elif (df[field_units].str == 'M').bool(): # 1 M = 1000000000 nM
        df[field_values] *= 1000000000
    
    else: # 1 mM = 1000000 nM
        df[field_values] *= 1000000
    
    return df

But I got this:

AttributeError: 'bool' object has no attribute 'bool'

Could someone please explain me that I did wrong?

>Solution :

the == operator returns a bool value. In your if and elif conditions you are trying to run the bool() method on a bool.

def convert_molars_bool(df, field_values, field_units):

    if (df[field_units].str == 'uM'): # 1 uM = 1000 nM
        df[field_values] *= 1000
    
    elif (df[field_units].str == 'M'): # 1 M = 1000000000 nM
        df[field_values] *= 1000000000
    
    else: # 1 mM = 1000000 nM
        df[field_values] *= 1000000
    
    return df

This should work

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