假设有两个数据帧共享相同的索引但具有不同的列。在这里合并两个数据帧还是连接更聪明?
import pandas as pd
from pandas import DataFrame
df1 = DataFrame(index = ['hey', 'yo'], columns = ['gee', 'thanks'], data = [[1,'foo'],[6,'rhy']])
df2 = DataFrame(index = ['hey', 'yo'], columns = ['youre', 'welcome'], data = [[8,'fotb'],[3,'yuo']])
#using merging
df3_merge = df1.merge(df2,left_index = True, right_index = True)
#result:
# gee thanks youre welcome
# hey 1 foo 8 fotb
# yo 6 rhy 3 yuo
#using concatenate
df3_concat = pd.concat([df1,df2], axis = 1)
#result:
# gee thanks youre welcome
# hey 1 foo 8 fotb
# yo 6 rhy 3 yuo
这个链接激发了这个问题。通常我总是使用,但我对别人使用或思考的东西感到好奇。concat
慕丝7291255
相关分类