简体   繁体   English

将两个 Pandas 数据框与一个公共列合并

[英]merging two pandas dataframe with one common column

I have two dataframes that look something like this:我有两个看起来像这样的数据框:

50       51           100            The Treasure of the Sierra Madre (1948)
51       52            98                                          Up (2009)
52       53           100             12 Angry Men (Twelve Angry Men) (1957)
53       54           100      The 400 Blows (Les Quatre cents coups) (1959)

                                    title viewers_score number_of_viewers_ratings
0  12 Angry Men (Twelve Angry Men) (1957)            97                    103672
1                     The 39 Steps (1935)            86                     23647
2     The Adventures of Robin Hood (1938)            89                     33584
3                    All About Eve (1950)            94                     44564
4   All Quiet on the Western Front (1930)            89                     17768

dataframes head from terminal来自终端的数据帧头

and I want to join them, I tried pandas 'merge':我想加入他们,我尝试了熊猫“合并”:

df_merge = pd.merge(df,df_,on='title)

but it doesn't work because the title columns in the dataframes aren't in the same order, so when I tried it, I get a false in all rows and so an empty dataframe.但它不起作用,因为数据帧中的标题列的顺序不同,所以当我尝试它时,我在所有行中都得到了一个错误,因此是一个空的数据帧。 how can I merge the two of them based on the common 'title' column?如何根据常见的“标题”列合并它们两个?

合并数据帧如下所示:

df_merge = df.merge(df_, on='title')
left_table.merge(right_table, on='title')

thats it.就是这样。 If you had an error, you didn't give much information to look at it.如果你有一个错误,你没有提供太多的信息来查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM