How to fix "TypeError: 'float' object is not iterable" when using the df.map function for a dataframe column (entire column)

227 views Asked by At

I'm trying to apply a function to every element in a column but I keep getting this error and I'm not sure how to fix it.

Code:

import pandas as pd
import pubchempy
import numpy as np

df = pd.read_csv("Data.tsv.txt", sep="\t")

.
.
.

df['CID'] = df['CID'].astype(str).apply(lambda x: x.replace('.0',''))

df['CID']= df['CID'].map(lambda x: get_properties(identifier=x, properties='MolecularWeight') if x>0 else pd.NA)

Error:

TypeError: '>' not supported between instances of 'str' and 'int'

Also, the get_properties() function is a function from pubchempy that takes the requested information (in this case, 'MolecularWeight') directly from the pubchem website.

The inputs are:

pubchempy.get_compounds(identifier, namespace=u'cid', searchtype=None, as_dataframe=False, **kwargs)

Only the properties and identifier parameters are required, the rest are optional.

Small Data Sample: enter image description here

Thanks in advance!

0

There are 0 answers