python - Comparing two pandas dataframes for differences -


i've got script updating 5-10 columns worth of data , start csv identical end csv instead of writing identical csvfile want nothing...

how can compare 2 dataframes check if they're same or not?

csvdata = pandas.read_csv('csvfile.csv') csvdata_old = csvdata  # ... stuff csvdata dataframe  if csvdata_old != csvdata:     csvdata.to_csv('csvfile.csv', index=false) 

any ideas?

you need careful create copy of dataframe, otherwise csvdata_old updated csvdata (since points same object):

csvdata_old = csvdata.copy() 

to check whether equal, can use assert_frame_equal in answer:

from pandas.util.testing import assert_frame_equal assert_frame_equal(csvdata, csvdata_old) 

you can wrap in function like:

try:     assert_frame_equal(csvdata, csvdata_old)     return true except:  # appeantly assertionerror doesn't catch     return false 

there discussion of better way...


Comments