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

Change NaN values with returned value of API

I need to change the NaN values in one column with the returned value of an API. I already wrote a function that calls the function and returns a value but not I am not sure how to change the values now. Would I need to loop through the whole DataFrame or are there other solutions? The dataframe looks like this:

colname1 colname2 colname3
1 2 3
4 5 6
7 8 9
NaN 11 12

My function takes values 11 and 12, and gives as return 10 to input in df["colname1"] for the last row. My question is how I can loop through the whole DataFrame to solve all such instances.

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 use apply. The best is to first subset the rows with NaNs to avoid requesting the API on the other values:

def api_func(a, b):
    return 10

# mask of the rows with NaN in colname1
m = df['colname1'].isna()

# output of API request
s = df.loc[m].apply(lambda r: api_func(r['colname2'], r['colname3']), axis=1)

# updating the column (in place)
df['colname1'].update(s)

output:

   colname1  colname2  colname3
0       1.0         2         3
1       4.0         5         6
2       7.0         8         9
3      10.0        11        12
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