Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient solutions insert or update row pandas

Tags:

python

pandas

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?

like image 919
user3605780 Avatar asked Apr 23 '26 11:04

user3605780


1 Answers

I think you need:

  • concat both DataFrames together
  • drop_duplicates for remove original rows with same values in column 0
  • sort_values by column 0
  • reset_index for unique index values

df = (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  []
like image 129
jezrael Avatar answered Apr 26 '26 01:04

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!