3 回答

TA贡献1801条经验 获得超16个赞
我认为问题出在不同的索引值上,所以解决方案是通过reset_index使用参数创建相同的drop=True:
final_df = pd.concat([train_df.reset_index(drop=True),
local_df["target"].reset_index(drop=True)], axis=1)
print(final_df.shape)
或设置local_dfby的索引train_df.index:
final_df = pd.concat([train_df,
local_df["target"].set_index(train_df.index)], axis=1)
print(final_df.shape)

TA贡献2011条经验 获得超2个赞
加入怎么样?:
import pandas as pd
df=pd.DataFrame({'a':[1,2,3],'b':[4,5,6]})
df2=pd.DataFrame({'c':[232,543,562]})
print(df.reset_index(drop=True).join(df2.reset_index(drop=True), how='left'))
输出:
a b c
0 1 4 232
1 2 5 543
2 3 6 562
添加回答
举报