I have two dataframes, a large one (df1) and a smaller one (df2). Both have the same columns and indexes.
df1 = pd.read_csv(milkProdFile, delimiter=';', dtype=str, names=cols).set_index(["ANIM", "NULACT"], drop=False)
df2 = pd.read_csv(correctionFile, delimiter=';', dtype=str, names=cols).set_index(["ANIM", "NULACT"], drop=False)
I wish to replace the rows from the large dataframe df1 with the matching rows (meaning the rows with matching indexes) from the small dataframe df2 using the update method from pandas.
df1.update(df2)
df1.to_csv("./corrected_lacts.txt", sep=';', header=False, index=False)
However, after executing this code, nothing happened. The corrected_lacts.txt text file still has the original values contained in the milkProdFile used to create df1.
Why is this happening and how can I fix it ?
EDIT :
vladislavAks' answer did the trick. I moved the set_index
to the end of my code and it worked. Thanks a lot !
CodePudding user response:
Try to .set_index
after update. I tried this code with example data, and it works:
df = pd.DataFrame({'ANIM': [1, 2, 3],
'NULACT': [400, 500, 600],
'target': [1, 1, 1]}) # without ".set_index(["ANIM", "NULACT"], drop=False)"
new_df = pd.DataFrame({'ANIM': [4, 5, 6],
'NULACT': [7, 8, 9],
'target': [0, 1, 0]})
df.update(new_df)
df.set_index(["ANIM", "NULACT"], drop=False)