根据pandas python的另一表中的一个值从一个表中获取值
我有下面的两个表:一个表是订单,另一个表是主表。两者都是python的熊猫中的数据框。
Orders
-------------
Date | item_id
--------------
672022| 123
672022| 976
672022| 532
--------------
Master Table
----------------------------------------------
item_id | Description | Supplier | Ship_From
----------------------------------------------
234 | Oranges | CWF | NY
341 | Nuts | DVR | NJ
532 | Grapes | ETT | CT
123 | Apples | ERH | CT
976 | Raspberry | HKQ | NY
731 | Bread | FBE | NJ
-----------------------------------------------
我希望我的最后一个表看起来像以下内容:
-------------------------------
Date | item_id | Description
-------------------------------
672022| 123 | Apples
672022| 976 | Raspberry
672022| 532 | Grapes
-------------------------------
我无法以简单的方式获得所需的输出,这是在Python中编写加入代码的最佳方法是什么? 我正在使用以下代码:
new = pd.merge(orders, master, on="item_id", how="left")
new = new[['Date', 'item_id', 'description']]
new.drop_duplicates(subset=None, keep='first', inplace=True)
I have the two tables below: One table is Orders and the other table is a Master table. Both are dataframes in Pandas, Python.
Orders
-------------
Date | item_id
--------------
672022| 123
672022| 976
672022| 532
--------------
Master Table
----------------------------------------------
item_id | Description | Supplier | Ship_From
----------------------------------------------
234 | Oranges | CWF | NY
341 | Nuts | DVR | NJ
532 | Grapes | ETT | CT
123 | Apples | ERH | CT
976 | Raspberry | HKQ | NY
731 | Bread | FBE | NJ
-----------------------------------------------
I want my final table to look like the below:
-------------------------------
Date | item_id | Description
-------------------------------
672022| 123 | Apples
672022| 976 | Raspberry
672022| 532 | Grapes
-------------------------------
I am unable to get the desired output in a simple way What is the best way to write joins code in Python?
I am using the below code:
new = pd.merge(orders, master, on="item_id", how="left")
new = new[['Date', 'item_id', 'description']]
new.drop_duplicates(subset=None, keep='first', inplace=True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效:
输出:
This should work:
output: