假设有两个数据帧共享相同的索引,但具有不同的列。将两个数据帧合并到此处或concat更明智吗
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
This link启发了这个问题。通常我总是使用concat
,但我很好奇别人用什么或怎么想
我想这取决于你需要什么
默认情况下,^{} 中的是
inner
连接,但可以将其更改为outer
、right
或left
在^{} 中默认为外部联接,但只能通过
inner
参数将其更改为inner
:另外,如果想要加入数据帧列表,更简单、更快的方法是
concat
如果希望左连接,则不能使用
concat
,因为未实现有关^{} 的更多信息
有关^{} 的更多信息
相关问题 更多 >
编程相关推荐