我正在嘗試合并兩個資料框,以便 DF3 中專案 ID 的每個實體都顯示與來自 DF1 的匹配 ID 關聯的定價資料。
DF3(我想要完成的事情)
| 食譜ID | itemID_out | qty_out | 購買價格 | 賣價 | 購買數量 | 銷售數量 | id_1_in | 數量_id1 | 購買價格 | 賣價 | 購買數量 | 銷售數量 | id_2_in | qty_id2 | 購買價格 | 賣價 | 購買數量 | 銷售數量 | id_3_in | 數量_id3 | 購買價格 | 賣價 | 購買數量 | 銷售數量 | id_4_in | qty_id4 | 購買價格 | 賣價 | 購買數量 | 銷售數量 | id_5_in | qty_id5 | 購買價格 | 賣價 | 購買數量 | 銷售數量 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1986年 | 1 | 129 | 167 | 67267 | 21637 | 123 | 1 | 10 | 15 | 1500 | 3000 | 124 | 1 | 12 | 14 | 550 | 800 | 125 | 1 | 8 | 12 | 124 | 254 | 126 | 1 | 22 | 25 | 1251 | 890 | 127 | 1 | 64 | 72 | 12783 | 1251515 |
| 2 | 1987年 | 1 | 1521 | 1675 | 654 | 1245 | 123 | 2 | 10 | 15 | 1500 | 3000 | ||||||||||||||||||||||||
| 3 | 1988年 | 1 | 128376 | 131429 | 47 | 23 | 123 | 10 | 10 | 15 | 1500 | 3000 | 124 | 3 | 12 | 14 | 550 | 800 |
這些是我試圖合并的兩個資料框。
DF1:包含26863行;專案名稱、ID 和價格資料的主串列。從 API 中提取,可以添加新專案,并在用戶提出更新請求后顯示為新行。
| 物品編號 | 姓名 | 購買價格 | 賣價 | 購買數量 | 銷售數量 |
|---|---|---|---|---|---|
| 1986年 | XYZ | 129 | 167 | 67267 | 21637 |
| 123 | 美國廣播公司 | 10 | 15 | 1500 | 3000 |
| 124 | 國防軍 | 12 | 14 | 550 | 800 |
DF2(包含 12784 行;從主串列中的專案組合的配方。從 API 中提取,可以添加新配方,并在用戶提出更新請求后顯示為新行。)
| 食譜ID | itemID_out | qty_out | id_1_in | 數量_id1 | id_2_in | qty_id2 | id_3_in | 數量_id3 | id_4_in | qty_id4 | id_5_in | qty_id5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1986年 | 1 | 123 | 1 | 124 | 1 | 125 | 1 | 126 | 1 | 127 | 1 |
| 2 | 1987年 | 1 | 123 | 2 | ||||||||
| 3 | 1988年 | 1 | 123 | 10 | 124 | 3 |
配方可以包含 1 到 5 個專案(出現空值)的組合,這些專案由 DF1?? 中的 ID 和/或 DF2 中的 itemID_out 列組成。
DF2 中的“id_#_in”列可以包含“itemID_out”列中的專案 ID,因為該配方使用從另一個配方輸出的專案。
我嘗試使用以下方法合并它:
pd.merge(itemlist_modified, recipelist_modified, left_on='itemID', right_on='itemID_out')
但這只會導致一列想法按預期接收定價資料。
我覺得我正在嘗試為此使用錯誤的功能,非常感謝任何幫助!
提前致謝!
uj5u.com熱心網友回復:
不是一個漂亮的方法,但它首先將成分表融合為長格式,然后將其合并到 itemlist 表中
import pandas as pd
import numpy as np
itemlist_modified = pd.DataFrame({
'itemID': [1986, 123, 124],
'name': ['XYZ', 'ABC', 'DEF'],
'buy_price': [129, 10, 12],
'sell_price': [167, 15, 14],
'buy_quantity': [67267, 1500, 550],
'sell_quantity': [21637, 3000, 800],
})
recipelist_modified = pd.DataFrame({
'RecipeID': [1, 2, 3],
'itemID_out': [1986, 1987, 1988],
'qty_out': [1, 1, 1],
'id_1_in': [123, 123, 123],
'qty_id1': [1, 2, 10],
'id_2_in': [124.0, np.nan, 124.0],
'qty_id2': [1.0, np.nan, 3.0],
'id_3_in': [125.0, np.nan, np.nan],
'qty_id3': [1.0, np.nan, np.nan],
'id_4_in': [126.0, np.nan, np.nan],
'qty_id4': [1.0, np.nan, np.nan],
'id_5_in': [127.0, np.nan, np.nan],
'qty_id5': [1.0, np.nan, np.nan],
})
#columns which are not qty or input id cols
id_vars = ['RecipeID','itemID_out','qty_out']
#prepare dict to map column name to ingredient number
col_renames = {}
col_renames.update({'id_{}_in'.format(i 1):'ingr_{}'.format(i 1) for i in range(5)})
col_renames.update({'qty_id{}'.format(i 1):'ingr_{}'.format(i 1) for i in range(5)})
#melt reciplist into longform
long_recipelist = recipelist_modified.melt(
id_vars=id_vars,
var_name='ingredient',
).dropna()
#add a new column to specify whether each row is a qty or an id
long_recipelist['kind'] = np.where(long_recipelist['ingredient'].str.contains('qty'),'qty_in','id_in')
#convert ingredient names
long_recipelist['ingredient'] = long_recipelist['ingredient'].map(col_renames)
#pivot on the new ingredient column
reshape_recipe_list = long_recipelist.pivot(
index=['RecipeID','itemID_out','qty_out','ingredient'],
columns='kind',
values='value',
).reset_index()
#merge with the itemlist
priced_ingredients = pd.merge(reshape_recipe_list, itemlist_modified, left_on='id_in', right_on='itemID')
#pivot on the priced ingredients
priced_ingredients = priced_ingredients.pivot(
index = ['RecipeID','itemID_out','qty_out'],
columns = 'ingredient',
)
#flatten the hierarchical columns
priced_ingredients.columns = ["_".join(a[::-1]) for a in priced_ingredients.columns.to_flat_index()]
priced_ingredients.columns.name = ''
priced_ingredients = priced_ingredients.reset_index()
priced_ingredients部分輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/478543.html
