根据pandas python的另一表中的一个值从一个表中获取值

发布于 2025-02-05 09:16:11 字数 1116 浏览 3 评论 0原文

我有下面的两个表:一个表是订单,另一个表是主表。两者都是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2025-02-12 09:16:11

这应该有效:

import pandas as pd
import io

data1 = '''Date  | item_id
--------------
672022| 123
672022| 976
672022| 532'''

data2 = '''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'''

df1 = pd.read_csv(io.StringIO(data1), sep='\s*\|\s*', engine='python')
df2 = pd.read_csv(io.StringIO(data2), sep='\s*\|\s*', engine='python')
df1.merge(df2[['item_id', 'Description']], on='item_id', how='inner')

输出:

日期item_id说明
0672022123苹果
1672022976覆盆子
2672022532葡萄

This should work:

import pandas as pd
import io

data1 = '''Date  | item_id
--------------
672022| 123
672022| 976
672022| 532'''

data2 = '''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'''

df1 = pd.read_csv(io.StringIO(data1), sep='\s*\|\s*', engine='python')
df2 = pd.read_csv(io.StringIO(data2), sep='\s*\|\s*', engine='python')
df1.merge(df2[['item_id', 'Description']], on='item_id', how='inner')

output:

Dateitem_idDescription
0672022123Apples
1672022976Raspberry
2672022532Grapes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文