1

I have a large DataFrame and I want to change the column value 'Final_Status' for the matching fields:

#df_full:
EMG Final_Status
G05AT   Fail
G05AZ   Fail
O05AP   Pass
O05AZ   Fail
O15AP   Fail
O51AK   Fail
T05AP   Fail

#df_overwrite:
EMG Final_Status
G05AT   Pass
G05AZ   Pass

#final result:
#df_full:
EMG Final_Status
G05AT   Pass
G05AZ   Pass
O05AP   Pass
O05AZ   Fail
O15AP   Fail
O51AK   Fail
T05AP   Fail

I can do it by looping over all rows of the smaller df (df_overwrite) but loops make my code very slow. Is there another way?

2 Answers 2

2

Take advantage of the Index in pandas to force alignment, then you want update. You can .reset_index() if you want 'EMG' back as a column.

df_full = df_full.set_index('EMG')
df_overwrite = df_overwrite.set_index('EMG')

df_full.update(df_overwrite, overwrite=True)
print(df_full)

      Final_Status
EMG               
G05AT         Pass
G05AZ         Pass
O05AP         Pass
O05AZ         Fail
O15AP         Fail
O51AK         Fail
T05AP         Fail
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use loc here (assuming EMG is the index):

df_full.loc[df_overwrite.index] = df_overwrite

1 Comment

yes this also works, i had to modify a bit as i had more columns in df_full df_full.loc[df_ow.index,'Final_Status'] = df_ow['Final_Status']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.