0

So I have two dataframes and am adding a column to df1 from df2 by using pd.merge

It works fine, only with small problem that it adds 5 decimal. So to show it is like this:

df1
room | value
A    | 10
B    | 19

df2
name  | room | value | value2
Joe   |   A  |   10  | 10.00000
Peter |   B  |   19  | 19.00000

Can I combine the round().astype(int) with pd.merge?

Current:

final = pd.merge(left=df1, right=df2, on="room", how="left")

Idea (which gives error "Cannot convert non-finite values (NA or inf) to integer"):

final = pd.merge(left=df1, right=df2, on="room", how="left").round().astype(int)

In a previous case I extracted that row with .round().astype(int) and then added that row to a new df by using merge. So with that extra step it worked. Just wondering if a more direct way also exists.

Thanks for any advice.

Best regards

Mitch

Mitch
  • 23
  • 4

1 Answers1

0

Okay, just found this question:

Integer becomes decimal in merged dataframe using python pandas

So it makes sense. Therefore I have to replace first all "nan" with 0 and then do the int command. Long story short: the extra way is the way :) (.fillna(0))

Mitch
  • 23
  • 4
  • 1
    If you found a question/answer that answers yours, you should either delete your question or suggest that it is a duplicate and wait for someone to mark it as a duplicate. What you shouldn't do is answer your own question with a link to the duplicate. – piRSquared Nov 07 '18 at 15:55