我的第一個數據幀是這樣的:RowCol Value_PreKey1 234Key3 235Key2 235Key4 237我的第二個數據框是:RowCol Value_PostKey3 235Key1 334Key4 237Key2 435如何通過組合兩個數據幀來創建像下面這樣的第三個數據幀RowCol Value_Pre Value_PostKey1 234 334Key3 235 235Key2 235 435Key4 237 237如何在 Python 中開發此代碼?
1 回答

函數式編程
TA貢獻1807條經驗 獲得超9個贊
使用map:
df1['Value_Post'] = df1['RowCol'].map(df2.set_index('RowCol')['Value_Post'])
print (df1)
RowCol Value_Pre Value_Post
0 Key1 234 334
1 Key3 235 235
2 Key2 235 435
3 Key4 237 237
或者merge使用左連接,尤其是在必要時附加多個新列:
df = df1.merge(df2, on='RowCol', how='left')
添加回答
舉報
0/150
提交
取消