1
我有以下的玩具代码:大熊猫如何Concat的2个dataframes不失列标题
import pandas as pd
df = pd.DataFrame()
df["foo"] = [1,2,3,4]
df2 = pd.DataFrame()
df2["bar"]=[4,5,6,7]
df = pd.concat([df,df2], ignore_index=True,axis=1)
print(list(df))
输出:[0,1]
预期输出:[foo,bar]
(顺序并不重要)
有什么办法连接两个数据帧而不丢失原始列标题,如果我可以保证标题将是唯一的?
想到遍历列然后将它们添加到其中一个DataFrames,但是有没有熊猫函数或concat
参数,我不知道?
谢谢!
'传递ignore_index = True将删除所有名称引用.'您是否需要传递ignore_index? – umutto
正如@umutto所说的...离开'ignore_index = True'或者根本不传递它。 – piRSquared
好吧,似乎工作!如果您愿意以此作为答复,我会很乐意接受它! – Priyank