how to join two pandas dataframe on specific column -
i have 1st pandas dataframe looks this
order_id buyer_id caterer_id item_id qty_purchased 387 139 1 7 3 388 140 1 6 3 389 140 1 7 3 390 36 1 9 3 391 79 1 8 3 391 79 1 12 3 391 79 1 7 3 392 72 1 9 3 392 72 1 9 3 393 65 1 9 3 394 65 1 10 3 395 141 1 11 3 396 132 1 12 3 396 132 1 15 3 397 31 1 13 3 404 64 1 14 3 405 146 1 15 3 and 2nd dataframe looks this
item_id meal_type 6 veg 7 veg 8 veg 9 nonveg 10 veg 11 veg 12 veg 13 nonveg 14 veg 15 nonveg 16 nonveg 17 veg 18 veg 19 nonveg 20 veg 21 veg i want join 2 data frames on item_id column. final data frame should contain item_type has match item_id.
i doing following in python
pd.merge(segments_data,meal_type,how='left',on='item_id')
but gives me nan values
you have check types dtypes of both columns (names) join on.
if there different, can cast them, because need same dtypes. numeric columns string columns, looks numbers.
if there both same string types, maybe cast both of them int. problem can whitespaces:
segments_data['item_id'] = segments_data['item_id'].astype(int) meal_type['item_id'] = meal_type['item_id'].astype(int) pd.merge(segments_data,meal_type,how='left',on='item_id')
Comments
Post a Comment