Pandas-合并

1 pd.concat实现数据合并

  • pd.concat([data1, data2], axis=1)
    按照行或列进行合并,axis=0为列索引,axis=1为行索引
# 按照行索引进行
pd.concat([data, dummies], axis=1)

2 pd.merge合并

pd.merge(left, right, how='inner', on=None)
按照两组数据的共同键值对合并或者左右各自

  • left,right:DataFrame
  • on: 指定的共同键
  • how:按照什么方式连接
Merge method SQL Join Name Description
left LEFT OUTER JOIN Use keys from left frame only
right RIGHT OUTER JOIN Use keys from right frame only
outer FULL OUTER JOIN Use union of keys from both frames
inner INNER JOIN Use intersection of keys from both frames
# 默认内连接
result = pd.merge(left, right, on=['key1', 'key2'])
#左连接
result = pd.merge(left, right, how='left', on=['key1', 'key2'])
#右连接
result = pd.merge(left, right, how='right', on=['key1', 'key2'])
#外连接
result = pd.merge(left, right, how='outer', on=['key1', 'key2'])
上一篇:23. Merge k Sorted Lists (分治)


下一篇:788. 逆序对的数量