pandas 之数据合并concat

import pandas as pd
s1=pd.Series(['a','b'])
s2=pd.Series(['c','d'])  

concat( )  参数如下:

concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=None, copy=True)

s1

0    a
1    b

s2

0    c
1    d

pd.concat([s1,s2])   ### 默认axis=0, 竖着拼接; ignore_index=False,索引不忽略,保持原来的索引不变  

0    a
1    b
0    c
1    d

pd.concat([s1,s2],ignore_index=True)    ### 忽略原来的索引,重新生成新的索引

0    a
1    b
2    c
3    d

pd.concat([s1,s2],axis=1)  ###  axis=1,横着拼接

   0  1
0  a  c
1  b  d
pd.concat([s1,s2],axis=1,ignore_index=True)   ### axis=1,横着拼接,原来的索引不忽略
   0  1
0  a  c
1  b  d

注意,如果s1、s2的索引不一样,则竖着拼接,保持原来的索引

s1=pd.Series(['a','b'],index=list('ab'))
s2=pd.Series(['c','d'])
print(s1,s2)
a    a
b    b
dtype: object
0    c
1    d
dtype: object
print(pd.concat([s1,s2],axis=1,ignore_index=True))
     0    1
a    a  NaN
b    b  NaN
0  NaN    c
1  NaN    d

猜你喜欢

转载自blog.csdn.net/qq_21840201/article/details/80975157