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
0 10
1 12
2 13
df2
0
0 22
1 33
2 44
3 55
df3
0
0 90
1 94
res= pd.concat([df1,df2,df3])
res
0
0 10
1 12
2 13
0 22
1 33
2 44
3 55
0 90
1 94

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

res2= pd.concat([df1,df2,df3], ignore_index=True)
res2
0
0 10
1 12
2 13
3 22
4 33
5 44
6 55
7 90
8 94

2. concat横着拼接

用参数axis= 1,看例子:

res_heng= pd.concat([df1,df2,df3], axis=1)
res_heng
0 0 0
0 10.0 22 90.0
1 12.0 33 94.0
2 13.0 44 NaN
3 NaN 55 NaN
THE END
< <上一篇
下一篇>>