gpt4 book ai didi

python - pd.concat() 不在同一索引上合并

转载 作者:太空宇宙 更新时间:2023-11-03 15:40:12 25 4
gpt4 key购买 nike

我有一个 DataFrame 包含名为 fcst 的预测,如下所示:

             yhat        yhat_lower  yhat_upper
ds
2015-08-31 -0.443522 -19.067399 17.801234
2015-09-30 6.794625 -31.472186 46.667981
...

进行此转换后:

fcst2 = fcst["yhat"].to_frame().rename(columns={"yhat":"test1"})
fcst3 = fcst["yhat"].to_frame().rename(columns={"yhat":"test2"})

我想在日期索引上将它们连接起来:

pd.concat([fcst2,fcst3])

但是我收到了一个没有在索引上对齐的 DataFrame:

             test1     test2
ds
2015-08-31 -0.443522 NaN
2015-09-30 6.794625 NaN
... ... ...
2017-05-31 NaN 95.563262
2017-06-30 NaN 85.829916

尽管如此:

(fcst2.index == fcst3.index).any()

返回真。

我的问题是:为什么两个 DataFrame 没有在索引上连接起来,我该怎么做才能解决这个问题?

我知道 join 函数,但由于某些日期会在我计划添加的其他一些 DataFrame 中丢失,我相信 concat 函数可能会更好。

最佳答案

它不起作用,因为 pd.concat 具有参数 axis=0 的默认值。因此,您可以按照 Dominique Paul 的建议使用 axis=1 调用函数,也可以改用函数 join。下面是一个例子:

# data to create the dataframes with
data_1 = [1,2,3,4,5]
index_1 = ['a','b','c','d','e']
data_2 = [6,7,8,9,10]
index_2 = ['b','d','e','a','c']

# create dataframes
df_1 = pd.DataFrame({'data_1':data_1, 'new_index':index_1})
df_2 = pd.DataFrame({'data_2':data_2, 'new_index':index_2})

# setting new index to test unaligned indexes
df_1.set_index('new_index', inplace=True, drop=True)
df_2.set_index('new_index', inplace=True, drop=True)

# join operation is performed on indexes
df_1.join(df_2)

关于python - pd.concat() 不在同一索引上合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52948106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com