连接两个数据帧会导致索引的列标题消失

问题描述 投票:0回答:1

我有两个数据帧 df_old 和 df_new。

df_old 有一个日期列和一堆其他列。 df_new 只有一个日期列,该列也被设置为其索引。

df_new 中的日期是 df_old 的超集

我想连接数据框,使 df_old 的列与 df_new 中的日期对齐,任何不对齐的列都会有 Nan。

df_new = pd.DataFrame()
df_new["date"] = pd.date_range(start='31/12/2023', end='31/12/2024')
df_new.set_index('date', inplace=True)
df_new = pd.concat([df_new , df_old], join="outer")

但是,执行此操作后,df_new 的“日期”列名称现在是空字符串。

这里有什么建议吗?我尝试通过索引引用 [0] 重命名该列,但没有喜悦

提前致谢

python pandas dataframe concatenation
1个回答
0
投票

df_new 只有一个日期列,它也被设置为其索引。”所以你几乎有一个索引(实际上是一个没有列的 DataFrame)。

因此你应该

reindex
:

out = df_old.reindex(df_new.index)
© www.soinside.com 2019 - 2024. All rights reserved.