免责声明
这是here,在那里我曾与包括pandas.DataFrames
几个项目一个pandas.Panel
一个后续问题。我想只有一个命令绘制在我DataFrame
从每个项目(在Panel
minor_axis)某列,避免代码簇状 plt.plot(x, DataFrame1[y1]) plt.plot(x, DataFrame2[y1]) ...
它带给因为我可以切换我的轴线在Panel
,这样,而不是一个答案一个项目包含一个数据集(具有一定起始参数的模拟)的所有信息,但是对于在其他项目(DataFrames
)中存储其他参数的所有不同模拟仅仅是一个信息(例如y值y1
)。优雅将数据添加到一个pandas.Panel一个模拟运行中
我基本的模拟代码
即使我的代码是模拟钟摆的行为,我会返回的值,而不是y1-y3
的真实的物理参数,打破它一般的模拟代码。该模拟将针对2个不同的起始参数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()
问题
我的问题是如何优雅(如代码尽可能几行)添加/每个模拟每个值追加到data
,考虑可能有5次或更多的模拟每个模拟步骤有10个或更多值?
E.g.在我的问题mentioned before我会创建一个新的DataFrame
并将其附加到给定模拟的现有数据集 - 类似data.append(pd.DataFrame([[x, y1, y2, y3]], columns=['x', 'y1', 'y2', 'y3']))
。但从那里我不能用一个命令正确地绘图,而是必须手动为每个模拟添加一个新图。
我会很高兴,如果有人可以帮助我了解如何建立一个Panel
这样“在运行” - 从我刚才的问题我已经知道如何绘制一个:)
UPDATE我被要求提供一些示例数据,但由于我想连续将我的模拟值添加到面板/项目中,而不是首先生成列表,因此我只能显示数据最终的样子。在开始时,面板应该是这样的:
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 [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
我通过希望这更清晰了,现在我正在寻找 - 如果不让我知道
的问题是,食谱始终把给定数据集,并从现有列表或字典生成其'DataFrames'和'Panels'。即使添加到它们中也是如此,但是又有一套完整的数据 - 但我想逐步追加到它上面,而且我在Cookbook中找不到这样的例子。 在伪代码中,我想要 '将给定参数k的模拟值的集合附加到各种DataFrame中,每个值在面板的一个额外项目中' – theGwiel
@theGwiel,它不是很清楚 - 你是什么要添加到现有的面板...你打算添加一个新的DataFrame? – MaxU
,我想出了是定义DataFrames的字典,你提出的(我没有找到一个选择,我可以将喜欢,当我张贴的问题的代码块),它看起来像这样的解决方法: 字符'D = { 'items0':pd.DataFrame(列= [ 'X', 'y1_sim1', 'y1_sim2']),...}' 并加入到这样一个项目:'d [ 'items0'] = d [ 'items0']。追加(pd.DataFrame([[X,Y 1]],列= [ 'X', 'Y1 _ {}'。格式(K)]),ignore_index =真)' ,其使用该密钥要添加到相应列的'k'参数。 – theGwiel