Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不能将行附加到面板对象内的数据帧?_Python_Dataframe_Append_Panel - Fatal编程技术网

Python 为什么不能将行附加到面板对象内的数据帧?

Python 为什么不能将行附加到面板对象内的数据帧?,python,dataframe,append,panel,Python,Dataframe,Append,Panel,如果我复制一个作为面板元素的dataframe,我可以使用append方法成功地更新它。但是,我无法成功更新作为面板元素的dataframe。如何更新作为面板元素的dataframe 这里是成功的附加到副本上 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-2

如果我复制一个作为面板元素的dataframe,我可以使用append方法成功地更新它。但是,我无法成功更新作为面板元素的dataframe。如何更新作为面板元素的dataframe

这里是成功的附加到副本上

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
如何更新面板元素dataframe

谢谢


Gato

这可能不是最好的答案,因此如果其他人有更好的方法,我很感兴趣,但我发现我可以删除元素,只使用一个条目创建一个新面板,然后在两个面板上进行合并。更新一个值似乎有很长的路要走,但它是有效的

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]: 
[204]中的
:pdata
出[204]:
尺寸:696(项目)x1(长轴)x6(短轴)
项目轴:2014年4月1日至2014年10月31日
长轴:0到0
次轴:起始价格的日期
在[209]:pdata['2014年8月20日']
出[209]:
日期交易结束价格证券开始头寸开始价格
2014年8月20日PE 25铜FP 3 17.4
[211]中:pdatatmp=pdata.drop('2014年8月20日')
In[212]:pdatatmp
出[212]:
尺寸:695(项目)x1(长轴)x6(短轴)
项目轴:2014年4月1日至2014年10月31日
长轴:0到0
次轴:起始价格的日期
[215]中:pdataupd=pd.Panel({'20-Aug-2014':test})
在[220]中:pdata=pd.concat([pdatatmp,pdataupd])
In[222]:pdata
出[222]:
尺寸:696(项目)x2(长轴)x6(短轴)
项目轴:2014年4月1日至2014年8月20日
长轴:0到1
次轴:起始价格的日期
在[223]中:pdata['2014年8月20日']
出[223]:
日期交易结束价格证券开始头寸开始价格
2014年8月20日PE 25铜FP 3 17.4
2014年8月20日PE 25铜FP 3 17.4
在[224]中:

第一个答案有效,但并不完全如预期的那样。元素已更新,但我发现panel希望其所有组件数据帧都具有相同的维度,因此在应答[0]中使用concat方法在所有其他元素数据帧中插入空记录

如果我们简单地省去面板构造并使用数据帧的dict,那么我们就有了所需的灵活性,并且更新工作效率很高

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]: 

事实证明,这是非常低效的,即使几千次更新也会永远运行,所以如果有人有更好的答案,我很感兴趣。
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]: 
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]: