2017-06-18 47 views
0

如果我复制作为面板元素的数据框,我可以使用append方法成功更新它。但是,我无法成功更新面板元素的数据框。我如何更新面板元素的数据框?为什么不能将行追加到面板对象内的数据框?

这里是复制成功追加。

In [204]: pdata 
Out[204]: 
<class 'pandas.core.panel.Panel'> 
Dimensions: 696 (items) x 1 (major_axis) x 6 (minor_axis) 
Items axis: 01-Apr-2014 to 31-Oct-2014 
Major_axis axis: 0 to 0 
Minor_axis axis: Date to Start Price 

In [198]: test = pdata['20-Aug-2014'] 

In [199]: test 
Out[199]: 
      Date Deal End Price Security Start Position Start Price 
0 20-Aug-2014 PE  25 CU FP    3  17.4 


In [200]: port.ix[233] 
Out[200]: 
Date    20-Aug-2014 
Security    CU FP 
Deal      PE 
Start Price    17.4 
Start Position    3 
End Price     25 
Name: 233, dtype: object 


In [201]: test.append(port.ix[233], ignore_index=True) 
Out[201]: 
      Date Deal End Price Security Start Position Start Price 
0 20-Aug-2014 PE  25 CU FP    3  17.4 
1 20-Aug-2014 PE  25 CU FP    3  17.4 

In [202]: test = test.append(port.ix[233], ignore_index=True) 

In [203]: test 
Out[203]: 
      Date Deal End Price Security Start Position Start Price 
0 20-Aug-2014 PE  25 CU FP    3  17.4 
1 20-Aug-2014 PE  25 CU FP    3  17.4 

在这里,我尝试了面板的元素同样的事情,但它不工作:

In [204]: pdata 
Out[204]: 
<class 'pandas.core.panel.Panel'> 
Dimensions: 696 (items) x 1 (major_axis) x 6 (minor_axis) 
Items axis: 01-Apr-2014 to 31-Oct-2014 
Major_axis axis: 0 to 0 
Minor_axis axis: Date to Start Price 

In [206]: pdata['20-Aug-2014'] = pdata['20-Aug-2014'].append(port.ix[233], ignore_index=True) 

In [207]: pdata['20-Aug-2014'] 
Out[207]: 
      Date Deal End Price Security Start Position Start Price 
0 20-Aug-2014 PE  25 CU FP    3  17.4 

在这里,我尝试分配数据帧的修改拷贝到元素面板,这也不起作用:

In [208]: pdata['20-Aug-2014'] = test 

In [209]: pdata['20-Aug-2014'] 
Out[209]: 
      Date Deal End Price Security Start Position Start Price 
0 20-Aug-2014 PE  25 CU FP    3  17.4 

如何我可以更新数据框是面板的元素吗?

感谢,

加托

回答

0

这可能不是最佳答案,让我很感兴趣,如果别人有更好的方法,但是我发现我可能下降的元素,使一个新的面板只是一个入口然后连接两个面板。似乎很长的时间来更新价值,但它的工作原理。

In [204]: pdata 
Out[204]: 
<class 'pandas.core.panel.Panel'> 
Dimensions: 696 (items) x 1 (major_axis) x 6 (minor_axis) 
Items axis: 01-Apr-2014 to 31-Oct-2014 
Major_axis axis: 0 to 0 
Minor_axis axis: Date to Start Price 
In [209]: pdata['20-Aug-2014'] 
Out[209]: 
      Date Deal End Price Security Start Position Start Price 
0 20-Aug-2014 PE  25 CU FP    3  17.4 

In [211]: pdatatmp = pdata.drop('20-Aug-2014') 

In [212]: pdatatmp 
Out[212]: 
<class 'pandas.core.panel.Panel'> 
Dimensions: 695 (items) x 1 (major_axis) x 6 (minor_axis) 
Items axis: 01-Apr-2014 to 31-Oct-2014 
Major_axis axis: 0 to 0 
Minor_axis axis: Date to Start Price 

In [215]: pdataupd = pd.Panel({'20-Aug-2014': test}) 

In [220]: pdata = pd.concat([pdatatmp, pdataupd]) 

In [222]: pdata 
Out[222]: 
<class 'pandas.core.panel.Panel'> 
Dimensions: 696 (items) x 2 (major_axis) x 6 (minor_axis) 
Items axis: 01-Apr-2014 to 20-Aug-2014 
Major_axis axis: 0 to 1 
Minor_axis axis: Date to Start Price 

In [223]: pdata['20-Aug-2014'] 
Out[223]: 
      Date Deal End Price Security Start Position Start Price 
0 20-Aug-2014 PE  25 CU FP    3  17.4 
1 20-Aug-2014 PE  25 CU FP    3  17.4 

In [224]: 
+0

原来这是非常低效的,甚至几千更新永远运行,所以我感兴趣,如果任何人有更好的答案。 – gatomulato

0

首先回答的作品,但并不完全符合市场预期。该元素已更新,但我发现该面板期望其所有组件数据框都具有相同的维度,因此在answer [0]中使用方法concat会在所有其他元素数据框中插入空记录。

如果我们简单地放弃面板构造并使用数据框的字典,那么我们就具有了我们所需的灵活性,并且更新可以高效地工作。

In [98]: pdata = {k: pstub for k in pdates['Dates'].values} 

In [106]: port.ix[7] 
Out[106]: 
Date    10-Jan-2014 
Security    LIFE US 
Deal     TMO US 
Start Price    75.8 
Start Position   7.5 
End Price    76.1 
Name: 7, dtype: object 

In [107]: pdata['10-Jan-2014'] = pdata['10-Jan-2014'].append(port.ix[7], ignore_index=True) 

In [108]: pdata['09-Jan-2014'] 
Out[108]: 
Empty DataFrame 
Columns: [Date, Deal, End Price, Security, Start Position, Start Price] 
Index: [] 

In [109]: pdata['10-Jan-2014'] 
Out[109]: 
      Date Deal End Price Security Start Position Start Price 
0 10-Jan-2014 TMO US  76.1 LIFE US    7.5   75.8 

In [110]: 
相关问题