How can I only get a numerical answer when applying a function to a dataframe?

37 Views Asked by At

This is the code I have until now:

import pandas as pd
import pubchempy
import numpy as np

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

from pubchempy import get_properties

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

df = df.drop(df[df.CID=='nan'].index)

df = df.drop( df.index.to_list()[150:] ,axis = 0 )


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

print(df)

The output that I'm getting under the 'CID' column is this:

CID

[{'CID': 5339, 'MolecularWeight': '398.4'}]

What can I do so that I only get the numerical 'MolecularWeight' value in the 'CID' column (eg. 398.4 in column one etc)?

0

There are 0 best solutions below