Skip to content Skip to sidebar Skip to footer

Concatenate Two Dataframes And Drop Duplicates In Pandas

For df2 which only has data in the year of 2019: type year value 0 a 2019 13 1 b 2019 5 2 c 2019 5 3 d 2019 20 df1 has multiple years data:

Solution 1:

Add DataFrame.drop_duplicates for get last rows per type and date after concat.

Solution working if type and date pairs are unique in both DataFrames.

df = (pd.concat([df1, df2], ignore_index=True, sort =False)
        .drop_duplicates(['type','date'], keep='last'))

Post a Comment for "Concatenate Two Dataframes And Drop Duplicates In Pandas"