Im trying to merge to panda dataframes, although what I want may not actually be a merge.
I have a two columns in two frames that match, one column shares unique values that can be used to join. the other column has one empty field and one populated one.
I want to overwrite the emtpy fields whilst matching on the unique fields but only keep the column thats overwritten, I do not want the rest of the columns from the second DataFrame.
hopefully the below will explain a little further
>>> animals = [{"animal" : "dog", "name" : "freddy", "food" : ""},{"animal" : "cat", "name" : "dexter", "food" : ""},{"animal" : "dog", "name" : "lou lou", "food" : ""}]
>>> foods = [{"name" : "freddy", "food" : "dog mix", "brand" : "doggys dog"},{"name" : "dexter", "food" : "fussy cat mix", "brand" : "fish fishy"},{"name" : "lou lou", "food" : "bones", "brand" : "i was a cow"}]
>>> a_pd = pd.DataFrame(animals)
>>> a_pd
animal food name
0 dog freddy
1 cat dexter
2 dog lou lou
>>> f_pd = pd.DataFrame(foods)
>>> f_pd
brand food name
0 doggys dog dog mix freddy
1 fish fishy fussy cat mix dexter
2 i was a cow bones lou lou
>>>
>>>
>>> animal_data = a_pd.merge(f_pd, on='name', how='left')
>>> animal_data
animal food_x name brand food_y
0 dog freddy doggys dog dog mix
1 cat dexter fish fishy fussy cat mix
2 dog lou lou i was a cow bones
>>>
I should just have food and I dont want the brand (also to note this is sample data and the live data has a lot more columns
desired results
>>> animal_data
animal name food
0 dog freddy dog mix
1 cat dexter fussy cat mix
2 dog lou lou bones