优雅地将数据添加到 pandas .正在运行的模拟中的面板 [英] Elegantly adding data to a pandas.Panel within a running simulation

查看:73
本文介绍了优雅地将数据添加到 pandas .正在运行的模拟中的面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自此处的后续问题,其中我有一个pandas.Panel,其中包含由pandas.DataFrames组成的多个项目.我只想在一个命令中从每个项目中在DataFrame中的某个列(在Panel中的minor_axis)中绘制某个列,避免像 plt.plot(x, DataFrame1[y1]) plt.plot(x, DataFrame2[y1]) ... 答案是,我可以在Panel中切换轴,以便代替包含一个数据集(具有特定起始参数的模拟)的一个数据集的所有信息的项目,而仅包含一个信息(例如yvalue y1)用于所有不同的模拟,并在其他项目(DataFrames)中存储其他参数.

This is a follow-up question from here, where I had a pandas.Panel with several items consisting of pandas.DataFrames. I wanted to plot a certain column in my DataFrame (minor_axis in the Panel) from each item in only one command, avoiding a code cluster like plt.plot(x, DataFrame1[y1]) plt.plot(x, DataFrame2[y1]) ... It was brought as an answer that I could switch my axes in the Panel so that instead of one item containing all the information of one dataset (of a simulation with a certain starting parameter), but rather just one information (e.g. yvalue y1) for all the different simulations an storing other parameters in other items (DataFrames).

即使我的代码是模拟摆的行为,我还是将其分解为带有返回值y1-y3而不是实际物理参数的常规模拟代码.将针对2个不同的起始参数k进行此仿真.

Even though my code is to simulate the behaviour of a pendulum I'll break it down to a general simulation code with returned values y1-y3 instead of the real physical parameters. This simulation will be done for 2 different starting parameters k.

import pandas as pd

data = pd.Panel(major_axis=[], minor_axis=['x', 'sim1', 'sim2'])

# some kind of simulation resulting in 3 simulated values and with a
# starting parameter for different simulation "strengths"
# not sure whether to use a list or dict here
ks = {'sim1' = 0.5, 'sim2' = 1.0}
for k in ks:
    x, y1, y2, y3 = 0, 0, 0, 0
    while x<100:
        x += 1
        y1 += 1*ks[k]*x
        y2 += 2*ks[k]*x
        y3 += 3*ks[k]*x
        ...

# for example the y2 value for the different k values should be plottable like this 
data['y2'].plot()


问题

我现在的问题是,考虑到可能存在5个或更多的模拟,而每个模拟步骤有10个或更多的值,如何优雅地(尽可能少的代码行)将每个模拟的每个值添加/追加到data?


Question

My question now is how to elegantly (as few lines of code as possible) add/append each value for each simulation to data, considering there could be 5 or more simulations with 10 or more values for each simulation step?

例如在我前面提到的问题中 创建一个新的DataFrame并将其附加到给定模拟的现有数据集中-类似于data.append(pd.DataFrame([[x, y1, y2, y3]], columns=['x', 'y1', 'y2', 'y3'])).但是从那里我无法用一个命令正确绘制,而不得不为每个模拟手动添加一个新图形.

E.g. in my problem mentioned before I'd create a new DataFrame and append it to my existing dataset for the given simulation - something like data.append(pd.DataFrame([[x, y1, y2, y3]], columns=['x', 'y1', 'y2', 'y3'])). But from there I couldn't plot properly with a single command but rather had to add a new graph for each simulation manually.

如果有人可以帮助我理解如何运行"构建这样的Panel,我会非常高兴-从我之前的问题中我已经知道如何绘制一个:c

I'd be very happy if someone could help me understand how to build a Panel like this "on the run" - from my previous question I already know how to plot one :)


更新,我被要求提供一些示例数据,但是由于我想将模拟值连续添加到面板/项目中,而不是先生成列表,因此我只能在表格中显示数据的外观.结尾.首先,面板应如下所示:


UPDATE I was asked for some example data, but since I want to consecutively add my simulated values into an Panel/item instead of generating a list first, I can only show how the data should look like in the end. In the beginning the Panel should look like this:

In [1]: print(data)
Out[1]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 0 (major_axis) x 3 (minor_axis)
Items axis: y1 to y2
Major_axis axis: None
Minor_axis axis: x to sim2

下面显示了模拟的工作方式,例如y1项目的最终外观如何

In the following is shown how the simulations works and how for example the y1-item should look like in the end

In [2]: ks = {'sim1' : 0.5, 'sim2' : 1.0}
Out[2]: {'sim1': 0.5, 'sim2': 1.0}

In [3]:
for k in ks:
    x, y1, y2 = 0, 0, 0

    while x<3:
        x += 1
        y1 += 1*ks[k]*x
        y2 += 2*ks[k]*x
        # HERE is missing what I'm looking for
        # it should append e.g. the y1 value to data['y1'] for both k
Out[3]: ...

In [4]: print(data['y1'])
Out[4]:         
     x    sim1    sim2
0    1    0.5     1.0
1    2    1.5     3.0
2    3    3.0     6.0

我希望通过它可以现在更清楚我正在寻找的东西-如果没有让我知道

I hope through this it's clearer now what I'm looking for - if not let me know

推荐答案

我认为构建Pandas.Panel的简便方法是构建以下形式的字典:

I think the easies way to build a Pandas.Panel would be to build a dictionary of the following form:

d = {
    'items_axis_element0': DataFrame0,
    'items_axis_element1': DataFrame1,
    'items_axis_element2': DataFrame2,
    ...
}

现在您可以轻松地建立一个面板:

now you can easily build up a Panel:

p = pd.Panel(d)

您可能会在《熊猫食谱》 中找到一些有用的示例

You may find some usefull examples in Pandas Cookbook

更新:以下是《熊猫食谱》中稍作修改的示例:

UPDATE: here is slightly modified example from Pandas Cookbook:

rng = pd.date_range('1/1/2013',periods=100,freq='D')
data = np.random.randn(100, 4)
cols = ['A','B','C','D']
df1, df2, df3 = pd.DataFrame(data, rng, cols), pd.DataFrame(data, rng, cols), pd.DataFrame(data, rng, cols)

pf = pd.Panel({'df1':df1,'df2':df2})

In [21]: pf
Out[21]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 100 (major_axis) x 4 (minor_axis)
Items axis: df1 to df2
Major_axis axis: 2013-01-01 00:00:00 to 2013-04-10 00:00:00
Minor_axis axis: A to D

现在我们可以添加df3如下:

now we can add df3 as follows:

In [22]: pf.join(pd.Panel({'df3':df3}))
Out[22]:
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 100 (major_axis) x 4 (minor_axis)
Items axis: df1 to df3
Major_axis axis: 2013-01-01 00:00:00 to 2013-04-10 00:00:00
Minor_axis axis: A to D

这篇关于优雅地将数据添加到 pandas .正在运行的模拟中的面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆