Difference between pd.merge() and dataframe.merge()

2024/9/21 20:52:34

I'm wondering what the difference is when you merge by pd.merge versus dataframe.merge(), examples below:

pd.merge(dataframe1, dataframe2)

and

dataframe1.merge(dataframe2)
Answer

We've two functions at our disposal for almost the same task pandas.merge() and DataFrame.merge().

pandas.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None)DataFrame.merge(right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes='_x', '_y', copy=True, indicator=False, validate=None)

Both look similar, what's the advantage of using one over the other?

pd.merge() calls for df.merge, so df1.merge(df2) will give almost same results as pd.merge(df1, df2).

However, pd.merge() is wrapping style function and df1.merge() is chaining style, which makes the later easier to chain from left to right

E.g.,

 df1.merge(df2).merge(df3) #looks better and readable [analogus to %>% pipeline operator in R] than pd.merge(pd.merge(df1, df2), df3).

Let's Look at a reproducible example

d1 = pd.read_html('https://worldpopulationreview.com/countries')
pop = d1[0]
print(pop.info(), '\n') #Data for 232 countries for 7 columnspop.head(3)d2 = pd.read_html('https://worldpopulationreview.com/country-rankings/median-age')
age = d2[0]
print(age.info(), '\n') #Data for 221 countries for 5 columnsage.head(3)display('pd.merge(): ', pd.merge(pop, age), 'df.merge(): ', pop.merge(age))
https://en.xdnf.cn/q/71909.html

Related Q&A

ctypes in python crashes with memset

I am trying to erase password string from memory like it is suggested in here.I wrote that little snippet:import ctypes, sysdef zerome(string):location = id(string) + 20size = sys.getsizeof(string)…

Python __del__ does not work as destructor? [duplicate]

This question already has answers here:What is the __del__ method and how do I call it?(5 answers)Closed 4 years ago.After checking numerous times, I did find inconsistent info about the topic.In some…

How to set default button in PyGTK?

I have very simple window where I have 2 buttons - one for cancel, one for apply. How to set the button for apply as default one? (When I press enter, "apply" button is pressed)However, I wa…

Can Python recognize changes to a file that it is running interactively?

I was doing some troubleshooting and I was curious if it is possible to run a Python script interactively, change a function defined in the script, save the file, then have the interactive shell recogn…

How to use FTP with Pythons requests

Is it possible to use the requests module to interact with a FTP site? requests is very convenient for getting HTTP pages, but I seem to get a Schema error when I attempt to use FTP sites. Is there …

How to find a best fit distribution function for a list of data?

I am aware of many probabilistic functions builted-in Python, with the random module. Id like to know if, given a list of floats, it would be possible to find the distribution equation that best fits t…

How to use peewee limit()?

With Peewee Im trying to use limit as follows:one_ticket = Ticket.select().limit(1) print one_ticket.count()This prints out 5 however. Does anybody know whats wrong here?

Python (1..n) syntax?

I see in the code on this Sage wiki page the following code:@interact def _(order=(1..12)):Is this (1..n) syntax unique to Sage or is it something in Python? Also, what does it do?

in python , How to load just one time a ML model in a rest service like django or flask?

I have a ML model saved in a pkl (pickel file), I have no problem loading this model and using it for prediction, even I have a rest service that expose it, the only problem is that I load the model i…

Adding a Title or Text to a Folium Map

Im wondering if theres a way to add a title or text on a folium map in python?I have 8 maps to show and I want the user to know which map theyre looking at without having to click on a marker. I attem…