df.merge(df_new, ...
df = df.merge(df_new, ...
The above code computes an intermediate value and assigns it to df
, discarding the old df
contents. It's similar to the df_final
assignment statement, but all in one go, without using a temporary variable.
This behavior is documented in the official Pandas documentation:
Returns: DataFrame A DataFrame of the two merged objects.
Note the absence of an inplace=
boolean option.
While merge()
doesn't update the original DataFrame in-place, you can still assign the merged DataFrame to the original variable to update its contents:
df1 = df1.merge(df2, left_on='lkey', right_on='rkey')
However, this is not the default behavior, and it's important to be aware of the distinction between the two.
Some authors and lecturers may describe merge()
as updating the original DataFrame in-place, but this is technically inaccurate. It's always a good idea to refer to the official documentation for accurate information about a function's behavior.