Home > Enterprise >  Change NaN values with returned value of API
Change NaN values with returned value of API

Time:04-13

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.

CodePudding user response:

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
  • Related