[英]Merging pandas dataframes with same columns
I'm trying to merge two data frames, one containing details of the win odds for a horse race and one with the place odds. 我正在尝试合并两个数据框,其中一个包含赛马获胜赔率的详细信息,一个包含地方赔率的细节。 They share some of the same column names.
它们共享一些相同的列名。 I am currently getting duplicates on the horse's names, with odds that I don't recognize.
我目前在马匹的名字上得到了重复,但赔率我不认识。 I think the problem is occurring because some races don't have a place market, only a win market so it's confusing things.
我认为问题之所以出现,是因为有些比赛没有地方市场,只有胜利市场,这使事情变得混乱。 Below is an example of the winning table on the left, place on the right.
下面是左侧中奖桌的示例,右侧中奖。
Time Horse Odds Result Time Horse Odds_P Result_P
13:55 Go faster 5.0 1 13:55 Go faster 5.0 1
14:10 Slow down 4.0 0
I want to just be able to add the Odds_P and Result_P for going faster on to the end of the win data frame and drop the entry that has no corresponding Place market. 我只想添加Odds_P和Result_P,以便更快地进入获胜数据帧的末尾,并删除没有相应的地方市场的条目。
I've tried concat, join and merge, all of which present me with me some duplicates of horse names, with odds that I don't recognize. 我已经尝试过concat,join和merge,所有这些都为我呈现了一些马名的副本,但赔率我却不认识。
Any help would be greatly appreciated 任何帮助将不胜感激
If I understand you correctly, your odds_p dataframe looks like: 如果我对您的理解正确,则您的odds_p数据帧如下所示:
Time Horse Odds Result
0 13.55 Go faster 5.0 1
1 14:10 Slow down 4.0 0
And result_p looks like: 和result_p看起来像:
Time Horse Odds_P Result_P
0 13.55 Go faster 5.0 1
I will rename the last two columns from odds_p dataframe to Odds_P and Result_P: 我将把odds_p数据帧的最后两列重命名为Odds_P和Result_P:
odds_p.rename(columns = {'Odds':'Odds_P', 'Result':'Result_P'}, inplace = True)
And now merge both using a RIGHT join: 现在使用RIGHT联接合并两个:
final_p = pd.merge(odds_p, result_p, on = ['Time', 'Horse', 'Odds_P', 'Result_P'], how = 'right')
Your final output will look like: 您的最终输出将如下所示:
Time Horse Odds_P Result_P
0 13.55 Go faster 5.0 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.