连接具有多重索引列和不规律时间戳的Pandas数据框

3 投票
1 回答
3177 浏览
提问于 2025-04-18 10:19

我有很多不同的数据框,它们都放在一个列表里,每个数据框的列都有多重索引,并且它们代表不同时间段的时间序列。我想做三件事:

  1. 把所有这些不同的数据框合在一起
  2. 如果有数据框的多重索引列是一样的,就把它们按时间轴拼接并排序
  3. 如果数据框的多重索引列不一样,就在列的方向上拼接(也就是横着拼)

我知道默认情况下,`pandas.concat(objs, axis=1)`是把列合并在一起,并且会对行索引进行排序,但我还希望那些标签和层级相同的数据框能够沿着时间轴连接,而不是完全并排放在一起。

我还要提到的是,这些有相同标签和层级的数据框是覆盖在不同时间段上的,它们之间是有联系的,但并不重叠。

举个例子:

first,second,third = rand(5,2),rand(5,2),rand(10,2)

a = pd.DataFrame(first, index=pd.DatetimeIndex(start='1990-01-01', periods=5, freq='d'))
a.columns = pd.MultiIndex.from_tuples([('A','a'),('A','b')])

b = pd.DataFrame(second, index=pd.DatetimeIndex(start='1990-01-06', periods=5, freq='d'))
b.columns = pd.MultiIndex.from_tuples([('A','a'),('A','b')])

c = pd.DataFrame(third, index=pd.DatetimeIndex(start='1990-01-01', periods=10, freq='d'))
c.columns = pd.MultiIndex.from_tuples([('B','a'),('B','b')])

pd.concat([a,b,c], axis=1)

结果是这样的:

Out[3]:
    A   B
    a   b   a   b   a   b
1990-01-01  0.351481    0.083324    NaN     NaN     0.060026    0.124302
1990-01-02  0.486032    0.742887    NaN     NaN     0.570997    0.633906
1990-01-03  0.145066    0.386665    NaN     NaN     0.166567    0.147794
1990-01-04  0.257831    0.995324    NaN     NaN     0.630652    0.534507
1990-01-05  0.446912    0.374049    NaN     NaN     0.311473    0.727622
1990-01-06  NaN     NaN     0.920003    0.051772    0.731657    0.393296
1990-01-07  NaN     NaN     0.142397    0.837654    0.597090    0.833893
1990-01-08  NaN     NaN     0.506141    0.056407    0.832294    0.222501
1990-01-09  NaN     NaN     0.655442    0.754245    0.802421    0.743875
1990-01-10  NaN     NaN     0.195767    0.880637    0.215509    0.857576

有没有简单的方法可以得到这个结果呢?

d = a.append(b)
pd.concat([d,c], axis=1)

Out[4]:
    A   B
    a   b   a   b
1990-01-01  0.351481    0.083324    0.060026    0.124302
1990-01-02  0.486032    0.742887    0.570997    0.633906
1990-01-03  0.145066    0.386665    0.166567    0.147794
1990-01-04  0.257831    0.995324    0.630652    0.534507
1990-01-05  0.446912    0.374049    0.311473    0.727622
1990-01-06  0.920003    0.051772    0.731657    0.393296
1990-01-07  0.142397    0.837654    0.597090    0.833893
1990-01-08  0.506141    0.056407    0.832294    0.222501
1990-01-09  0.655442    0.754245    0.802421    0.743875
1990-01-10  0.195767    0.880637    0.215509    0.857576

关键是我不知道这些数据框在列表中的顺序,所以我基本上需要一个能判断什么时候用concat(obj, axis=1)或concat(obj, axis=0)的东西,来合并我的数据框列表。也许pandas里已经有可以做到这一点的功能?

1 个回答

3

我不太确定有没有一种一行代码就能做到这一点(可能有)……
在这种情况下,我会考虑先创建一个空的框架,然后再填充它:

In [11]: frames = [a, b, c]

获取它们的索引和列的并集:

In [12]: index = sum(x.index for x in frames)
         cols = sum(x.columns for x in frames)

In [13]: res = pd.DataFrame(index=index, columns=cols)

然后用每个框架(通过标签)来填充这个空框架:

In [14]: for df in [a, b, c]:
             res.loc[df.index, df.columns] = df

In [15]: res
Out[15]:
                     A                     B
                     a           b         a         b
1990-01-01   0.8516285   0.4087078  0.577000  0.595293
1990-01-02   0.6544393   0.4377864  0.851378  0.595919
1990-01-03   0.3123428  0.03825423  0.834704  0.989195
1990-01-04   0.2314499   0.4971448  0.343455  0.770400
1990-01-05   0.1982945   0.9031414  0.466225  0.463490
1990-01-06   0.7370323   0.3923151  0.263120  0.892815
1990-01-07  0.09038236   0.8778266  0.643816  0.049769
1990-01-08   0.7199705  0.02114493  0.766267  0.472471
1990-01-09  0.06733081    0.443561  0.984558  0.443647
1990-01-10   0.4695022   0.5648693  0.870240  0.949072

撰写回答