如果您使用接受的答案,您将丢失列名称,如接受的答案中所示example,并在文档(强调):
生成的轴将标记为 0, ..., n - 1。如果您要在串联轴所在的位置串联对象,这会很有用not具有有意义的索引信息。
它看起来像列名称('Name column'
)对原始海报/原始问题有意义。
要保存列名称,请使用pandas.concat
, but don't ignore_index
(默认值为ignore_index
is False
;所以你可以完全忽略这个论点)。继续使用axis=1
:
import pandas
# Note these columns have 3 rows of values:
original = pandas.DataFrame({
'Age':[10, 12, 13],
'Gender':['M','F','F']
})
# Note this column has 4 rows of values:
additional = pandas.DataFrame({
'Name': ['Nate A', 'Jessie A', 'Daniel H', 'John D']
})
new = pandas.concat([original, additional], axis=1)
# Identical:
# new = pandas.concat([original, additional], ignore_index=False, axis=1)
print(new.head())
# Age Gender Name
#0 10 M Nate A
#1 12 F Jessie A
#2 13 F Daniel H
#3 NaN NaN John D
请注意 John D 没有年龄或性别。