Notification texts go here Contact Us Buy Now!

Pandas merge() has no effect on dataframe

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.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.