pandas.concat-按照特定轴连接pandas对象

语法格式

pandas.concat(objs*axis=0join='outer'ignore_index=Falsekeys=Nonelevels=Nonenames=Noneverify_integrity=Falsesort=Falsecopy=True)

常用的几个参数解释:

  • objs: Series或数据框映射或序列,比较常见的用法是传递DadaFrame对象组成的list
  • axis: 按行(0)或列(1)连接,默认为0
  • ingnore_index: 布尔值,默认为False,表示concat连接对象中依然保留了原来的index。如果设为True,则concat连接对象会重新生成0, …, n - 1这样的index。

代码示例

import pandas as pd
df1=pd.DataFrame({"A":[2,3],"B":[7,9]})
df2=pd.DataFrame({"C":[3,5]})

#按行连接两个数据框,生成对象使用df1,df2原有的index
con1=pd.concat([df1,df2],0)
print(con1)
#      A    B    C
# 0  2.0  7.0  NaN
# 1  3.0  9.0  NaN
# 0  NaN  NaN  3.0
# 1  NaN  NaN  5.0

#按行连接两个数据框,ignore_index=True生成对象重新生成index
con2=pd.concat([df1,df2],0,ignore_index=True)
print(con2)
#      A    B    C
# 0  2.0  7.0  NaN
# 1  3.0  9.0  NaN
# 2  NaN  NaN  3.0
# 3  NaN  NaN  5.0

#使用keys选项在数据的最外层添加层次索引,并给索引添加label值
con3=pd.concat([df1,df2],0,keys=["df1","df2"],names=["df name","ID"])
print(con3)
#               A    B    C
# df name ID
# df1     0   2.0  7.0  NaN
#         1   3.0  9.0  NaN
# df2     0   NaN  NaN  3.0
#         1   NaN  NaN  5.0
posted @ 2023-03-16 08:54  yayagogogo  阅读(22)  评论(0编辑  收藏  举报