多个数据帧应具有相同的索引值。如何比较?

Several dataframes should have the same index values. How to compare?

我有几个数据框。都是时间序列索引。 问题是,有些缺少数据,我需要检查哪些具有不同的日期索引。 现在都包含大约相同的值,只有一个或两个具有附加值或缺少一些值的情况,在这种情况下,我需要调用不同的数据提供者以获取其余数据帧的这些值所以算法不会出错。

INDEX    VAL

DF1
01-01-17 6.00
03-01-17 4.53
05-01-17 8.91

DF2
01-01-17 4.11
03-01-17 8.67
07-01-17 1.93
09-01-17 3.11

DF3
01-01-17 4.00
03-01-17 20.10
07-01-17 3.12

DF4
01-01-17 8.11
03-01-17 1.89
06-01-17 3.89
07-01-17 4.89

有数百个值,很难手工完成。

我想我正在寻找的是一种比较它们或创建一种矩阵的方法,该矩阵向我显示我需要为每个 df 获得哪些值。

两种可能性:

1) 查看dataframe.resample。这将确保您在每个数据框中具有相同的索引值;任何缺失的行都将设置为 nan。

2) 使用 dataframe.join to combine the dataframes. With the how keyword you can specify the type of merging. In your case, 'outer' may be the right choice if you don't have a "master index". There is a good description of merge, join, and concatenate 可用。

让我们尝试使用 列表理解

  • 列表理解
  • pd.concat
  • reindex
  • eval
  • 突出显示的数据框样式

:

list_of_df = ['df1','df2','df3','df4']
concat_df = pd.concat([eval(i) for i in list_of_df])
pd.concat([eval(i).reindex(pd.date_range(concat_df.index.min(), 
                                         concat_df.index.max(), 
                                         freq='MS'))
                  .rename(columns={1:i}) for i in ['df1','df2','df3','df4']], axis=1)\
  .style.applymap(lambda x: 'background: yellow' if pd.isnull(x) else '')