pandas: 複数の列で 2 つのデータ フレームをマージ (結合) する 質問する

pandas: 複数の列で 2 つのデータ フレームをマージ (結合) する 質問する

2 つの列を使用して 2 つの pandas データフレームを結合しようとしています。

new_df = pd.merge(A_df, B_df,  how='left', left_on='[A_c1,c2]', right_on = '[B_c1,c2]')

しかし、次のエラーが発生しました:

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4164)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4028)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13166)()

pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13120)()

KeyError: '[B_1, c2]'

これを実行する正しい方法は何でしょうか?

ベストアンサー1

これを試して

new_df = pd.merge(A_df, B_df,  how='left', left_on=['A_c1','c2'], right_on = ['B_c1','c2'])

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

left_on : 左の DataFrame で結合するラベルまたはリスト、または配列のようなフィールド名。列の代わりに特定のベクトルを結合キーとして使用するには、DataFrame の長さのベクトルまたはベクトルのリストにすることができます。

right_on : ラベルまたはリスト、または配列のようなフィールド名。右の DataFrame またはベクトル/ベクトルのリストで結合します (left_on ドキュメントによる)

おすすめ記事