熊猫:合并分层数据

时间:2014-07-15 09:25:48

标签: python pandas merge dataframe

我正在寻找一种方法将具有复杂层次结构的数据合并到一个pandas DataFrame中。这种层次结构是由数据中不同的相互依赖性引起的。例如。有一些参数可以定义数据的生成方式,然后是时间依赖的可观察量,空间相关的可观察量,以及依赖于时间和空间的可观察量。

更明确:假设我有以下数据。

#  Parameters
t_max = 2
t_step = 15
sites = 4

# Purely time-dependent
t = np.linspace(0, t_max, t_step)
f_t = t**2 - t

# Purely site-dependent
position = np.array([[0, 0], [1, 0], [0, 1], [1, 1]])  # (x, y)
site_weight = np.arange(sites)

# Time-, and site-dependent.
occupation = np.arange(t_step*sites).reshape((t_step, sites))

# Time-, and site-, site-dependent
correlation = np.arange(t_step*sites*sites).reshape((t_step, sites, sites))

(最后我会有很多这些数据集。每组参数一个。)

现在,我想把所有这些藏在熊猫DataFrame中。我想最终的结果看起来像这样:

| ----- parameters ----- | -------------------------------- observables --------------------------------- |
|                        |                                        | ---------- time-dependent ----------- |
|                        | ----------- site-dependent --- )       ( ------------------------ |            |
|                        |                                | - site2-dependent - |                         |
| sites | t_max | t_step | site | r_x | r_y | site weight | site2 | correlation | occupation | f_t | time |

我认为部分重叠的层次结构可能无法实现。如果它们是隐含的,那么我可以得到,例如,它是可以的。所有依赖于站点的数据,通过以特定方式索引DataFrame。

此外,如果您认为有更好的方法在Pandas中安排这些数据,请随时告诉我。

问题

如何构建包含所有上述数据的DataFrame,并以某种方式反映相互依赖关系(例如f_t取决于time,但不取决于site )。所有这些都是足够通用的,这样很容易添加或删除某些可观察的,可能有新的相互依赖性。 (例如,取决于第二个时间轴的数量,如时间 - 时间相关性。)


到目前为止我得到了什么

在下文中,我将向您展示我自己走了多远。但是,我不认为这是实现上述目标的理想方式。特别是,因为它在添加或删除某些可观察量方面缺乏一般性。

指数

鉴于上述数据,我首先定义了我将需要的所有多指数。

ind_time = pd.Index(t, name='time')
ind_site = pd.Index(np.arange(sites), name='site')
ind_site_site = pd.MultiIndex.from_product([ind_site, ind_site], names=['site', 'site2'])
ind_time_site = pd.MultiIndex.from_product([ind_time, ind_site], names=['time', 'site'])
ind_time_site_site = pd.MultiIndex.from_product([ind_time, ind_site, ind_site], names=['time', 'site', 'site2'])

个人DataFrame s

接下来,我创建了各个数据块的数据帧。

df_parms = pd.DataFrame({'t_max': t_max, 't_step': t_step, 'sites': sites}, index=[0])
df_time = pd.DataFrame({'f_t': f_t}, index=ind_time)
df_position = pd.DataFrame(position, columns=['r_x', 'r_y'], index=ind_site)
df_weight = pd.DataFrame(site_weight, columns=['site weight'], index=ind_site)
df_occupation = pd.DataFrame(occupation.flatten(), index=ind_time_site, columns=['occupation'])
df_correlation = pd.DataFrame(correlation.flatten(), index=ind_time_site_site, columns=['correlation'])

index=[0]中的df_parms似乎是必要的,因为否则Pandas只会抱怨标量值。实际上,我可能会用这个特定模拟运行时间戳来替换它。这至少会传达一些有用的信息。

Merge Observables

在数据框可用的情况下,我将所有可观察量加入一个大DataFrame

df_all_but_parms = pd.merge(
  pd.merge(
    pd.merge(
      df_time.reset_index(),
      df_occupation.reset_index(),
      how='outer'
    ),
    df_correlation.reset_index(),
    how='outer'
  ),
  pd.merge(
    df_position.reset_index(),
    df_weight.reset_index(),
    how='outer'
  ),
  how='outer'
)

在我目前的方法中,这是我最不喜欢的一点。 merge函数仅适用于数据帧对,并且要求它们至少具有一个公共列。所以,我必须要小心加入我的数据帧的顺序,如果我要添加一个正交的observable,那么我就无法将它与其他数据合并,因为它们不会共享一个公共列。是否有一个可用的功能只需在数据帧列表上进行一次调用即可获得相同的结果?我试过了concat,但它不会合并公共列。因此,我最终得到了许多重复的timesite列。

合并所有数据

最后,我将数据与参数合并。

pd.concat([df_parms, df_all_but_parms], axis=1, keys=['parameters', 'observables'])

到目前为止,最终结果如下:

         parameters                 observables                                                                       
              sites  t_max  t_step         time       f_t  site  occupation  site2  correlation  r_x  r_y  site weight
    0             4      2      15     0.000000  0.000000     0           0      0            0    0    0            0
    1           NaN    NaN     NaN     0.000000  0.000000     0           0      1            1    0    0            0
    2           NaN    NaN     NaN     0.000000  0.000000     0           0      2            2    0    0            0
    3           NaN    NaN     NaN     0.000000  0.000000     0           0      3            3    0    0            0
    4           NaN    NaN     NaN     0.142857 -0.122449     0           4      0           16    0    0            0
    ..          ...    ...     ...          ...       ...   ...         ...    ...          ...  ...  ...          ...
    235         NaN    NaN     NaN     1.857143  1.591837     3          55      3          223    1    1            3
    236         NaN    NaN     NaN     2.000000  2.000000     3          59      0          236    1    1            3
    237         NaN    NaN     NaN     2.000000  2.000000     3          59      1          237    1    1            3
    238         NaN    NaN     NaN     2.000000  2.000000     3          59      2          238    1    1            3
    239         NaN    NaN     NaN     2.000000  2.000000     3          59      3          239    1    1            3

正如您所看到的,这不能很好地工作,因为实际上只为第一行分配了参数。所有其他行只有NaN s代替参数。但是,由于这些是所有数据的参数,因此它们也应包含在此数据框的所有其他行中。

作为一个小问题:如果我将上述数据框存储在hdf5中,大熊猫会有多聪明。我最终会得到大量重复数据,还是会避免重复存储?


更新

感谢Jeff's answer我能够将所有数据推送到一个通用合并的数据框架中。基本的想法是,我所有的可观测量都已经有了一些共同的列。即,参数。

首先,我将参数添加到我的所有可观测数据中。数据帧。

all_observables = [ df_time, df_position, df_weight, df_occupation, df_correlation ]
flat = map(pd.DataFrame.reset_index, all_observables)
for df in flat:
    for c in df_parms:
        df[c] = df_parms.loc[0,c]

然后我可以通过缩减将所有这些合并在一起。

df_all = reduce(lambda a, b: pd.merge(a, b, how='outer'), flat)

其结果具有所需的形式:

         time       f_t  sites  t_max  t_step  site  r_x  r_y  site weight  occupation  site2  correlation
0    0.000000  0.000000      4      2      15     0    0    0            0           0      0            0
1    0.000000  0.000000      4      2      15     0    0    0            0           0      1            1
2    0.000000  0.000000      4      2      15     0    0    0            0           0      2            2
3    0.000000  0.000000      4      2      15     0    0    0            0           0      3            3
4    0.142857 -0.122449      4      2      15     0    0    0            0           4      0           16
5    0.142857 -0.122449      4      2      15     0    0    0            0           4      1           17
6    0.142857 -0.122449      4      2      15     0    0    0            0           4      2           18
..        ...       ...    ...    ...     ...   ...  ...  ...          ...         ...    ...          ...
233  1.857143  1.591837      4      2      15     3    1    1            3          55      1          221
234  1.857143  1.591837      4      2      15     3    1    1            3          55      2          222
235  1.857143  1.591837      4      2      15     3    1    1            3          55      3          223
236  2.000000  2.000000      4      2      15     3    1    1            3          59      0          236
237  2.000000  2.000000      4      2      15     3    1    1            3          59      1          237
238  2.000000  2.000000      4      2      15     3    1    1            3          59      2          238
239  2.000000  2.000000      4      2      15     3    1    1            3          59      3          239

通过重新索引数据,层次结构变得更加明显:

df_all.set_index(['t_max', 't_step', 'sites', 'time', 'site', 'site2'], inplace=True)

导致

                                             f_t  r_x  r_y  site weight  occupation  correlation
t_max t_step sites time     site site2                                                          
2     15     4     0.000000 0    0      0.000000    0    0            0           0            0
                                 1      0.000000    0    0            0           0            1
                                 2      0.000000    0    0            0           0            2
                                 3      0.000000    0    0            0           0            3
                   0.142857 0    0     -0.122449    0    0            0           4           16
                                 1     -0.122449    0    0            0           4           17
                                 2     -0.122449    0    0            0           4           18
...                                          ...  ...  ...          ...         ...          ...
                   1.857143 3    1      1.591837    1    1            3          55          221
                                 2      1.591837    1    1            3          55          222
                                 3      1.591837    1    1            3          55          223
                   2.000000 3    0      2.000000    1    1            3          59          236
                                 1      2.000000    1    1            3          59          237
                                 2      2.000000    1    1            3          59          238
                                 3      2.000000    1    1            3          59          239

1 个答案:

答案 0 :(得分:2)

我认为你应该做这样的事情,把df_parms作为你的索引。这样,您可以轻松地使用不同的参数连接更多帧。

In [67]: pd.set_option('max_rows',10)

In [68]: dfx = df_all_but_parms.copy()

您需要将列分配给框架(您也可以直接构建多索引,但这是从您的数据开始的。)

In [69]: for c in df_parms.columns:
             dfx[c] = df_parms.loc[0,c]

In [70]: dfx
Out[70]: 
         time       f_t  site  occupation  site2  correlation  r_x  r_y  site weight  sites  t_max  t_step
0    0.000000  0.000000     0           0      0            0    0    0            0      4      2      15
1    0.000000  0.000000     0           0      1            1    0    0            0      4      2      15
2    0.000000  0.000000     0           0      2            2    0    0            0      4      2      15
3    0.000000  0.000000     0           0      3            3    0    0            0      4      2      15
4    0.142857 -0.122449     0           4      0           16    0    0            0      4      2      15
..        ...       ...   ...         ...    ...          ...  ...  ...          ...    ...    ...     ...
235  1.857143  1.591837     3          55      3          223    1    1            3      4      2      15
236  2.000000  2.000000     3          59      0          236    1    1            3      4      2      15
237  2.000000  2.000000     3          59      1          237    1    1            3      4      2      15
238  2.000000  2.000000     3          59      2          238    1    1            3      4      2      15
239  2.000000  2.000000     3          59      3          239    1    1            3      4      2      15

[240 rows x 12 columns]

设置索引(这将返回一个新对象)

In [71]: dfx.set_index(['sites','t_max','t_step'])
Out[71]: 
                        time       f_t  site  occupation  site2  correlation  r_x  r_y  site weight
sites t_max t_step                                                                                 
4     2     15      0.000000  0.000000     0           0      0            0    0    0            0
            15      0.000000  0.000000     0           0      1            1    0    0            0
            15      0.000000  0.000000     0           0      2            2    0    0            0
            15      0.000000  0.000000     0           0      3            3    0    0            0
            15      0.142857 -0.122449     0           4      0           16    0    0            0
...                      ...       ...   ...         ...    ...          ...  ...  ...          ...
            15      1.857143  1.591837     3          55      3          223    1    1            3
            15      2.000000  2.000000     3          59      0          236    1    1            3
            15      2.000000  2.000000     3          59      1          237    1    1            3
            15      2.000000  2.000000     3          59      2          238    1    1            3
            15      2.000000  2.000000     3          59      3          239    1    1            3

[240 rows x 9 columns]
相关问题