1

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?

3
  • 1
    what is B? in the question Commented Jun 19, 2017 at 16:38
  • 1
    I'm very confused by this question Commented Jun 19, 2017 at 16:41
  • I'm very sorry guys, I changed the dataframes but forgot to change the text. Now it is alright. Commented Jun 19, 2017 at 17:00

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

I was able to do it using <code>results=pd.DataFrame(results).T</code> and <code>df=df.combine_first(results)</code>. Thanks for the combine_first tip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.