#concatenating
df1 = pd.DataFrame(np.ones((3,4))*0,columns = ['A','B','C','D'])
df2 = pd.DataFrame(np.ones((3,4))*1,columns = ['A','B','C','D'])
df3 = pd.DataFrame(np.ones((3,4))*2,columns = ['A','B','C','D'])
print(df1,df2,df3,'#####')
res = pd.concat([df1,df2,df3],axis = 0)
print(res)
res = pd.concat([df1,df2,df3],axis = 0,ignore_index=True)#忽略索引号,重新统一加索引
print(res)
# join,['inner','outer']默认值是outer
df1 = pd.DataFrame(np.ones((3,4))*0,columns = ['A','B','C','D'],index = [1,2,3])
df2 = pd.DataFrame(np.ones((3,4))*1,columns = ['B','C','D','E'],index = [2,3,4])#部分重合
print(df1)
print(df2)
res = pd.concat([df1,df2])#outer
print(res)
res = pd.concat([df1,df2],join = 'inner')#合并重叠的部分
print(res)
#join_axes
df1 = pd.DataFrame(np.ones((3,4))*0,columns = ['A','B','C','D'],index = [1,2,3])
df2 = pd.DataFrame(np.ones((3,4))*1,columns = ['B','C','D','E'],index = [2,3,4])#部分重合
res = pd.concat([df1,df2],axis=1,join_axes= [df1.index])#按df1的索引合并
print(res)
#append 默认是加行,竖向加
df1 = pd.DataFrame(np.ones((3,4))*0,columns = ['A','B','C','D'])
df2 = pd.DataFrame(np.ones((3,4))*1,columns = ['A','B','C','D'])
res = df1.append(df2)
print(res)
s1 = pd.Series([1,2,3,4],index =['A','B','C','D'])
res=df1.append(s1,ignore_index=True)
print(res)
结果显示:
A B C D
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0 A B C D
0 1.0 1.0 1.0 1.0
1 1.0 1.0 1.0 1.0
2 1.0 1.0 1.0 1.0 A B C D
0 2.0 2.0 2.0 2.0
1 2.0 2.0 2.0 2.0
2 2.0 2.0 2.0 2.0 #####
A B C D
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
0 1.0 1.0 1.0 1.0
1 1.0 1.0 1.0 1.0
2 1.0 1.0 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