my first data frame-
df1 = pd.DataFrame({'CONTRACT':['Tom', 'nick', 'krish', 'jack'], 'Net_Qty':[20, 21, 19, 18]}) CONTRACT Net_Qty 0 Tom 20 1 nick 21 2 krish 19 3 jack 18
second data frame-
df2 = pd.DataFrame({'CONTRACT':['Tom', 'nick', 'amit', 'joy'], 'Net_Qty':[30, 40, 45, 54]}) CONTRACT Net_Qty 0 Tom 30 1 nick 40 2 amit 45 3 joy 54
I want dataframe dataframe Like this (all values of df2 and uncommon values of df1)-
CONTRACT Net_Qty 0 Tom 30 1 nick 40 2 krish 19 4 jack 18 2 amit 45 3 joy 54
I tried like this-
cols = list(df1.columns) df1.loc[df1.CONTRACT.isin( df2.CONTRACT), cols] = df2[cols] print(df1)
but its not working fine…….
Can anyone please suggest a better way-
Advertisement
Answer
Use pd.concat
and drop_duplicates
:
out = pd.concat([df2, df1]).drop_duplicates('CONTRACT', ignore_index=True) print(out) # Output CONTRACT Net_Qty 0 Tom 30 1 nick 40 2 amit 45 3 joy 54 4 krish 19 5 jack 18