0


I was trying to find a solution but couldn't get there.
I have 2 data frames, one large one with a few thousand records and another with just a few rows that are exceptions - let's call it A and B respectively.

A and B have columns named "Month", "Town" and "Residents".

My objective is to override the values of column "Residents" in table A, with the "Residents" value from table B for the cases columns "Month" and "Town" match - kind of like a join in SQL.

I was trying to do it with a Numpy where. statement but got a few errors - like "ValueError: Can only compare identically-labeled Series objects".

Any ideas on how to solve it?

darthbith
  • 18,484
  • 9
  • 60
  • 76
Jo Costa
  • 421
  • 1
  • 6
  • 17

1 Answers1

1

To do a join you can use merge: C = A.merge(B, how='left', on=['Month', 'Town'])

Then, since you have Residents col in both, the merge will automatically add defaults of ('_x', '_y'). You can create a new Residents col using _y if present by:

C['Residents'] = C['Residents_y'].fillna(C['Residents_x']
hchw
  • 1,388
  • 8
  • 14