我有 2 个数据框,如下所示:
数据框1:
6 count
store_1 10
store_2 23
store_3 53
数据框2:
store_name location
store_1 location_a
store_2 location_b
我正在尝试加入上述两个数据框,以便得到以下输出:
6 count location
store_1 10 location_a
store_2 23 location_b
store_3 53
我正在尝试使用列的索引 ID 而不是按列名称将 Dataframe1
与 Dataframe2
合并。
最佳答案
索引上的左合并适合您。
data = {'6': ['store1', 'store2', 'store3'], 'count': [10, 23, 53]}
df1 = pd.DataFrame(data).set_index('6')
df1
data = {'store_name': ['store1', 'store2'], 'location': ['location_a', 'location_b']}
df2 = pd.DataFrame(data).set_index('store_name')
df2
df1.merge(df2, left_index=True, right_index=True, how='left')
希望这有帮助。
关于python - Pandas - 按索引 ID 合并两个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665599/