数据合并的concat()方法 ,他可以定义在哪个轴是合并。语法如下:
pd.concat(objs,axis = 0,join = 'outer',ignore_index = False,keys = None)
objs:Series或DataFrame数据组成的列表(list)或元组(tuples)
axis:连接合并的轴。值为0表示按行合并,1表示按列合并,默认为0
join:有2个值分别是‘outer’,‘inner’,分别表是向上索引的并集和交集,默认为‘outer’
ignore_index:是否忽略掉原来的数据索引
keys:定义索引,也支持默认标(从0开始的自然数)
concat()应用:
import pandas as pd
df1 = pd.DataFrame({'length':[5.1,4.0,2.5],'width':[3.5,0.3,2.7],'category':['test','test','test']})
df2 = pd.DataFrame({'length_1':[1.5,0.4,5.2],'width_1':[5.3,3.0,7.2],'category':['test_1','test_1','test_1']})
print(pd.concat([df1,df2]))
运行结果:
length width category length_1 width_1
0 5.1 3.5 test NaN NaN
1 4.0 0.3 test NaN NaN
2 2.5 2.7 test NaN NaN
0 NaN NaN test_1 1.5 5.3
1 NaN NaN test_1 0.4 3.0
2 NaN NaN test_1 5.2 7.2