假設我有一個看起來像這樣的表:
table = pd.DataFrame(
columns=["Name", "Size", "Color"],
data=[['A', 1, 'Red'], ['B', 2, 'Green'], ['C', 3, 'Blue']]
)
和一個看起來像這樣的查找表:
lookup = pd.DataFrame(
columns=["Color", "Source", "Lead Days"],
data=[["Red", "Europe", 2],
["Green", "Europe", 3],
["Blue", "US", 1],
["Yellow", "Europe", 2]]
)
我如何table通過從 中查找“顏色”來添加“來源”和“提前期”列lookup?
一個故事有時會有所幫助。
table 有我需要訂購的所有物品。
lookup 有我從哪里訂購它們以及需要多長時間。
我想進行轉換,table以便它可以顯示我需要訂購的每個專案的“來源”和“交貨天數”。
決賽桌應該是這樣的:
Note: while I'm sure there's a way to do this with merge, or top level table operations. In the spirit of Minimally Sufficient Pandas, and to avoid the huge kluge that is pandas' over-provisioning of operations, I'd prefer to do it with apply. Apply is nice because it's easy to consistently reach for apply in all situations.
Here's my current approach, but it results in the error ValueError: Columns must be same length as key
To me, this makes little sense, since I'm returning a list of length 2 and putting it into two columns. But I'm sure pandas has its reasons for being anti-intuitive here.
lookup_columns = ["Source", "Lead Days"]
table[lookup_columns] = table.apply(
lambda row:
lookup.query('`Color` == "{color}"'.format(color=row["Color"])).loc[:, lookup_columns].values[0]
, axis = 1)
uj5u.com熱心網友回復:
使用result_type="expand":
lookup_columns = ["Source", "Lead Days"]
table[lookup_columns] = table.apply(
lambda row:
lookup.query('`Color` == "{color}"'.format(color=row["Color"])).loc[:, lookup_columns].values[0]
, axis=1, result_type="expand")
print(table)
Name Size Color Source Lead Days
0 A 1 Red Europe 2
1 B 2 Green Europe 3
2 C 3 Blue US 1
從檔案(強調我的):
result_type {'expand', 'reduce', 'broadcast', None}, default None
這些只在axis=1(列)時起作用:
'expand':類似串列的結果將變成列。
uj5u.com熱心網友回復:
使用apply,您可以執行以下操作:
>>> pd.concat([table, table['Color'].apply(lambda x: lookup.loc[lookup['Color'] == x, ['Source', 'Lead Days']].squeeze())], axis=1)
Name Size Color Source Lead Days
0 A 1 Red Europe 2
1 B 2 Green Europe 3
2 C 3 Blue US 1
舊答案
使用pd.merge:
>>> pd.merge(table, lookup, how='left', on='Color')
Name Size Color Source Lead Days
0 A 1 Red Europe 2
1 B 2 Green Europe 3
2 C 3 Blue US 1
uj5u.com熱心網友回復:
我不會使用申請。它在計算上是昂貴的。對于您的情況,加入就足夠了。
table.set_index('Color').join(lookup.set_index('Color')).reset_index()
Color Name Size Source Lead Days
0 Red A 1 Europe 2
1 Green B 2 Europe 3
2 Blue C 3 US 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/312993.html
