idに2つのdictをマージしたい。 Dict xには、idごとに異なるIDと行数が多数含まれています。 Dict yには複数のキー値が含まれており、常にdict xより少ない行しか持たない。私が欲しいキー値でグルーピングされたdictsのリストをPythonでマージする
x = [{'costgroup': '1', 'POC1': '2', 'post': '5','id': '1'},
{'costgroup': '2', 'POC1': '1', 'post': '4','id': '1'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '1'},
{'costgroup': '1', 'POC1': '2', 'post': '5','id': '2'},
{'costgroup': '2', 'POC1': '1', 'post': '4','id': '2'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '2'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '2'}]
y = [{'id': '1', 'laminate': 'D'},
{ 'id':'2', 'laminate': T'}]
出力は以下の通りです:
z = [{'costgroup': '1', 'POC1': '2', 'post': '5','id': '1','laminate':'D'},
{'costgroup': '2', 'POC1': '1', 'post': '4','id': '1','laminate': 'D'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '1','laminate': 'D'},
{'costgroup': '1', 'POC1': '2', 'post': '5','id': '2','laminate': 'T'},
{'costgroup': '2', 'POC1': '1', 'post': '4','id': '2','laminate': 'T'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '2','laminate': 'T'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '2','laminate': 'T'}]
これはパンダ
dfx = pd.DataFrame(x)
dfy = pd.DataFrame(y)
pd.merge(dfx,dfy, how ='left', left_on = 'id', right_on = 'id')
を使用して達成するのは簡単です。しかし、私はAWSラムダ関数を使用して、この適用するつもりです私はパンダのオーバーヘッドを望んでおらず、アウトプットはディクテーションである必要があります。下のコードを試してみましたが、私はIDの別の値を見つけて反復するために何かを追加しなければなりませんでした。しかし、まだ必要な出力はありません。
valuelist = ['1']
def copyf(dictlist, key, valuelist):
return [d for d in dictlist if d[key] in valuelist]
y1 = copyf(y, 'id', valuelist)
x1 = copyf(x, 'id', valuelist)
y1.append(x1)
上記の内容は興味深いものですが、私が必要とするものではありません。
[{'distance': '2', 'id': '1', 'laminate': 'D'},
[{'POC1': '2', 'costgroup': '1', 'id': '1', 'post': '5'},
{'POC1': '1', 'costgroup': '2', 'id': '1', 'post': '4'},
{'POC1': '5', 'costgroup': '3', 'id': '1', 'post': '2'}]]
'ids'は' y'で一意ですか? – schwobaseggl
はいidsはyで一意です – Erich