pandas.concat实现竖着拼接、横着拼接DataFrame

1、concat竖着拼接(默认的竖着,axis=0)

话不多说,直接看例子:

import pandas as pd

df1=pd.DataFrame([10,12,13])
df2=pd.DataFrame([22,33,44,55])
df3=pd.DataFrame([90,94])
df1
0
010
112
213
df2
0
022
133
244
355
df3
0
090
194
res= pd.concat([df1,df2,df3])
res
0
010
112
213
022
133
244
355
090
194

如果要生成新索引,忽略原来索引怎么办?
默认有个参数ignore_index= False,将其值改为True
:

res2= pd.concat([df1,df2,df3], ignore_index=True)
res2
0
010
112
213
322
433
544
655
790
894

2、concat横着拼接

用参数axis= 1,看例子:

res_heng= pd.concat([df1,df2,df3], axis=1)
res_heng
000
010.02290.0
112.03394.0
213.044NaN
3NaN55NaN

版权声明:本文为u013317445原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。