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

标签 python python-3.x pandas dataframe

我有一个 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/

相关文章:

python - pandas.DataFrame 和 numpy.array 中的 np.isreal 行为不同

python - 删除数组项并更新数组索引

python - 在openpyxl中插入图像

python - 使用 PCA 特征作为 X 和 Y 轴绘制集群

python - 查找、连接和删除 python 列表中的项目

python - pandas 中的自定义 bin 和 sum ?

python - 打开jupyter notebook最快的方法

python - 如何将 .py 转换为 .exe(32 位)

python - 检测和排除 pandas DataFrame 中的异常值

pandas - python : Best way to visualize dict of dicts