concat方法相当于数据库中的全连接(union all)
pandas.concat(objs, axis=0, join=‘outer’, join_axes=None, ignore_index=False,keys=None, levels=None, names=None, verify_integrity=False)
参数:
1)axis
当axis=0时,pd.concat([obj1, obj2])的效果与obj1.append(obj2)是相同的;当axis=1时,pd.concat([obj1, obj2], axis=1)的效果与pd.merge(obj1, obj2, left_index=True, right_index=True, how=‘outer’)是相同的。
import pandas as pd
left = pd.DataFrame({
'Name': list('AB'),
'age': [28, 30]},
index=list(range(2)))
right = pd.DataFrame({
'Name': ['Billy', 'Brian', 'Bob'],
'class': ['sub2', 'sub6', 'sub5']},
index=list(range(1, 4)))
rs1 = pd.concat([left, right], keys=['x', 'y'])
rs2 = left.append(right)
print(rs1)
print("---"*16)
print(rs2)
cs1 = pd.concat([left, right], keys=['m', 'n'], axis=1)
cs2 = pd.merge(left, right, left_index=True, right_index=True, how='outer')
运行结果:
2)join
inner取交集,outer取并集
cs1 = pd.concat([left, right], keys=['m', 'n'], axis=1, join='inner')
cs2 = pd.concat([left, right], keys=['m', 'n'], axis=1)