我编写了下面的函数来在面板中创建3列(所有数据帧)。 for循环用于根据计算更新其值。
有没有办法在不使用for循环的情况下为所有数据帧更新这些列?
def addAdjustedColumns(symbols, quotes):
new_quotes = quotes.copy()
# add 3 columns to all dataframes inside the panel
new_quotes = quotes.transpose(2, 0, 1)
new_quotes['AdjOpen'] = 0.
new_quotes['AdjHigh'] = 0.
new_quotes['AdjLow'] = 0.
new_quotes = new_quotes.transpose(1, 2, 0)
for symbol in symbols:
new_quotes[symbol].AdjOpen = new_quotes[symbol].Open * (new_quotes[symbol].AdjClose / new_quotes[symbol].Close)
new_quotes[symbol].AdjHigh = new_quotes[symbol].High * (new_quotes[symbol].AdjClose / new_quotes[symbol].Close)
new_quotes[symbol].AdjLow = new_quotes[symbol].Low * (new_quotes[symbol].AdjClose / new_quotes[symbol].Close)
return new_quotes
谢谢,
JM
答案 0 :(得分:1)
以下是一些您可能会发现有用的面板操作。我组织面板(按照你的说法),符号(A到D)是minor_axis,字段(High,Low,Close)是项目,而日期是major_axis。这些都是大熊猫0.12
In [12]: p = tm.makePanel()
In [13]: p.items = ['High','Low','CLose']
In [14]: p
Out[14]:
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 30 (major_axis) x 4 (minor_axis)
Items axis: High to CLose
Major_axis axis: 2000-01-03 00:00:00 to 2000-02-11 00:00:00
Minor_axis axis: A to D
可以轻松地跨日期广播所有符号的现场操作
In [15]: p['HL'] = p['High']-p['Low']
In [16]: p
Out[16]:
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 30 (major_axis) x 4 (minor_axis)
Items axis: High to HL
Major_axis axis: 2000-01-03 00:00:00 to 2000-02-11 00:00:00
Minor_axis axis: A to D
添加符号,转置,添加和转置
In [17]: pa = p.transpose(2,0,1)
In [18]: pa
Out[18]:
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 4 (major_axis) x 30 (minor_axis)
Items axis: A to D
Major_axis axis: High to HL
Minor_axis axis: 2000-01-03 00:00:00 to 2000-02-11 00:00:00
In [19]: pa['E'] = pa['A'] + pa['D']
In [20]: p = pa.transpose(1,2,0)
In [21]: p
Out[21]:
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 30 (major_axis) x 5 (minor_axis)
Items axis: High to HL
Major_axis axis: 2000-01-03 00:00:00 to 2000-02-11 00:00:00
Minor_axis axis: A to E