I have a dataframe with the following structure:
0 1 2
0 0.00086076 500.00000000 []
1 0.00086075 819.00000000 []
2 0.00086072 1162.00000000 []
3 0.00086071 20.00000000 []
4 0.00086069 10170.00000000 []
5 0.00086067 18.00000000 []
Then I have another dataframe with update values:
0 1 2
0 0.00086071 50.00000000 []
1 0.00086068 81.00000000 []
It is sorted on column 0 and I need to merge the two as follows:
0 1 2
0 0.00086076 500.00000000 []
1 0.00086075 819.00000000 []
2 0.00086072 1162.00000000 []
3 0.00086071 50.00000000 []
4 0.00086069 10170.00000000 []
5 0.00086068 81.00000000 []
6 0.00086067 18.00000000 []
So if a value matches one in column 0 it should update column 1 if not it should insert a new row and sort again.
Is there an efficient way or predefined function for doing this?
I think you need:
concat both DataFrames togetherdrop_duplicates for remove original rows with same values in column 0sort_values by column 0reset_index for unique index valuesdf = (pd.concat([df1, df2])
.drop_duplicates([0] , keep='last')
.sort_values(0 , ascending=False)
.reset_index(drop=True))
print (df)
0 1 2
0 0.000861 500.0 []
1 0.000861 819.0 []
2 0.000861 1162.0 []
3 0.000861 50.0 []
4 0.000861 10170.0 []
5 0.000861 81.0 []
6 0.000861 18.0 []
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With