aabujamra
aabujamra

Reputation: 4636

Pandas/Python - update dataframe

I have the following dataframe:

df:

          Wins     Ratio
id
234         10      None
143         32      None
678          2      None

I'm running a model to find out Ratio for each id. My model is finding Ratio, it is in another data frame, that looks like this:

result:

143
Wins     32
Ratio   987

However, I'm struggling to update df with ratio. I'm looking for a function that simply updates df for the id 143. Tryed to use the pd.dataframe.update() but seems it doesn't work that way (or I was unable to make it work). Can someone help on that?

Upvotes: 1

Views: 57

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Where:

df

Outputs:

     Wins Ratio
id             
234    10  None
143    32  None
678     2  None

And:

result

Outputs:

       143
Wins    32
Ratio   98

You can update df using combine_first:

df.replace('None',np.nan).combine_first(result.T)

Output:

     Wins  Ratio
143    32   98.0
234    10    NaN
678     2    NaN

Upvotes: 1

Related Questions