pandas合并

concat

Concatenate pandas objects along a particular axis.

Allows optional set logic along the other axes.

Can also add a layer of hierarchical indexing on the concatenation
axis, which may be useful if the labels are the same (or overlapping)
on the passed axis number.
pandas.concat

pandas.concat(objs, *, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True)

Merge vertically by default
在这里插入图片描述

a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
b = pd.DataFrame(np.random.normal(0, 1, (5, 5)))
c = pd.concat([a, b], axis=1, join='inner', ignore_index=True)

a:
在这里插入图片描述
b:
在这里插入图片描述
c:
在这里插入图片描述

merge

Merge DataFrame or named Series objects with a database-style join.

A named Series object is treated as a DataFrame with a single named
column.

The join is done on columns or indexes. If joining columns on columns,
the DataFrame indexes will be ignored. Otherwise if joining indexes on
indexes or indexes on a column or columns, the index will be passed
on. When performing a cross merge, no column specifications to merge
on are allowed.
pandas.merge

pandas.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None)

You can use Pandas merge to implement vlookup like functions

a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
a['merge'] = [1, 2, 3]
b = pd.DataFrame(np.random.normal(0, 1, (5, 5)), index=[2, 9, 10, 11, 12])
b['merge'] = [2, 3, 4, 5, 6]
c = pd.merge(a, b, how='left', on='merge', suffixes=('_a', '_b'))

a:
在这里插入图片描述
b:
在这里插入图片描述
c:
在这里插入图片描述

join

Join columns of another DataFrame.

Join columns with other DataFrame either on index or on a key column.
Efficiently join multiple DataFrame objects by index at once by
passing a list.
pandas.DataFrame.join

DataFrame.join(other, on=None, how='left', lsuffix='', rsuffix='', sort=False, validate=None)

joins index-on-index by default

a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
a['merge'] = [1, 2, 3]
b = pd.DataFrame(np.random.normal(0, 1, (5, 5)), index=[2, 9, 10, 11, 12])
b['merge'] = [2, 3, 4, 5, 6]
c = a.join(b,  rsuffix='_b')

a:
在这里插入图片描述
b:
在这里插入图片描述
c:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值