私は2つのパンダデータフレームを持っています - df_current_data、df_new_data。pandasデータフレームのマージ(update insert)のためのより良い方法
私の目標は、マージを適用することです(パンダのマージ関数ではなく、 'update \ insert'のようにマージすることです)。一致するかどうかのチェックはキー列によるものです。
私の結果は、3つのオプションの行タイプで構築する必要があります。 df_current_dataに存在するが、df_new_dataに存在していない
行は - 「そのまま」の結果を挿入します。
df_new_dataには存在するが、df_current_dataには存在しない行は、結果として「そのまま」挿入されます。
df_new_dataに存在し、df_current_dataに存在する行 - 結果はdf_new_dataから行を取得する必要があります。
これは古典的なマージアップ動作です。
例:
# rows 0,1 are in current and not in new (check by index1 and index2)
# row 2 is common
In [41]: df_current_source
Out[41]: A index1 index2
0 1 1 4
1 2 2 5
2 3 3 6
# rows 0,2 are in current and not in new (check by index1 and index2)
# row 1 is common
In [42]: df_new_source
Out[42]: A index1 index2
0 4 2 7
1 5 3 6
2 6 4 5
# the result has 2 rows that only in current (rows 0,1)
# the result has 2 rows that only in new (rows 3,4)
# the result has one row that exists in both current and new (row 2 - index1 = 3, index2 = 6) - so the value of the column A is from the new and not from the current (5 instead of 2)
In [43]: df_result
Out[43]: A index1 index2
0 1 1 4
1 2 2 5
2 5 3 6
3 4 2 7
4 6 4 5
私がやったことだ:
# left join from source to new
df = df_current_source.merge(df_new_source, how='left', left_on=p_new_keys,
right_on=p_curr_keys, indicator=True)
# take only the rows that exists in the current and not exists in the source
df_only_current = df[df['_merge'] == 'left_only']
# merge new data into current data
df_result = pd.concat([df_only_current, df_new_source])
別のオプションは、ISIN機能である:
df_result = pd.concat([df_current_source[~df_current_source[p_key_col_name]\
.isin(df_new_source[p_key_col_name])], df_new_source])
問題は、私が1つの以上の鍵を持っている場合私はisinを使用することはできません、私はマージが必要です。
新しいものからの電流がはるかに大きいと仮定すると、最新の行で現在の一致する行を直接更新し、 "新しい"データフレームの新しい行を現在の行に追加することです。
しかし、私はそれを行う方法がわかりません。
ありがとうたくさんありがとうございます。
出力:あなたはcombine_firstを使用できますか?インデックス? –
また、入力と期待される出力を提供してください。 –
更新の意味は?私は2つの列が一致する必要があります。 入力と予想される出力を追加しました。 – user2671057