0
我正在對一些股票進行事件研究,這導致熊貓DataFrame中的列是股票股票代碼(SPY,GOOG,AAPL等),指數是時間戳。 DataFrame內的單元格的值爲NaN或1.我想根據事件DataFrame生成一個訂單DataFrame。因爲我想爲每個單元格== 1創建一個訂單,所以我認爲applymap是合適的。但是,看起來使用applymap剝去了索引和列的單元格。我試了下面的代碼:在DataFrame上使用applymap,但保留索引/列信息
def appendOrder(orders, value):
if value == 1:
index = ["Year", "Month", "Day", "Stock", "OrderType", "Amount"]
s = pd.Series(index=index)
s["Stock"] = value.index
def createOrders(events):
columns = ["Year", "Month", "Day", "Stock", "OrderType", "Amount"]
orders = pd.DataFrame(columns=columns)
events.applymap(lambda x: appendOrder(orders,x))
上面的代碼在appendOrder方法中斷了,因爲value沒有索引。
有沒有辦法在datamap上使用applymap時保留索引和列信息?
編輯
這裏是事件數據框的一個片段:
SPY GOOG AAPL XOM
2013-10-1-16:00:00 NaN 1 NaN 1
2013-10-2-16:00:00 NaN NaN NaN NaN
2013-10-3-16:00:00 NaN NaN NaN NaN
2013-10-4-16:00:00 1 NaN NaN NaN
2013-10-5-16:00:00 NaN NaN NaN NaN
2013-10-6-16:00:00 1 NaN 1 NaN
2013-10-7-16:00:00 NaN NaN NaN NaN
2013-10-8-16:00:00 NaN 1 NaN NaN
我想打開上述事件數據框到下面的訂單數據框:
Year Month Day Stock OrderType Amount
0 2013 10 1 GOOG Buy 100
1 2013 10 1 XOM Buy 100
2 2013 10 4 SPY Buy 100
3 2013 10 6 SPY Buy 100
4 2013 10 6 AAPL Buy 100
5 2013 10 8 GOOG Buy 100
我希望能讓它更清楚一些。
您可以發佈您的數據樣本以及您希望輸出的樣子嗎?我不確定applymap是你想要的。 – TomAugspurger