语法格式
pandas.concat(objs, *, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=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)
#按行连接两个数据框,ignore_index=True生成对象重新生成index
con2=pd.concat([df1,df2],0,ignore_index=True)
print(con2)
#使用keys选项在数据的最外层添加层次索引,并给索引添加label值
con3=pd.concat([df1,df2],0,keys=["df1","df2"],names=["df name","ID"])
print(con3)
输出结果
con1=pd.concat([df1,df2],0)
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
con2=pd.concat([df1,df2],0,ignore_index=True)
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
con3=pd.concat([df1,df2],0,keys=["df1","df2"],names=["df name","ID"])
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
标签:index,NaN,df1,df2,连接,pd,pandas,concat From: https://www.cnblogs.com/chaimy/p/17218078.html