簡體   English   中英

帶有Data_Time數據轉換的Python Financial OHLC:將數組列表轉換為Pandas,並為每個列表追加列名

[英]Python Financial OHLC with Data_Time data conversion: List Array to Pandas and appending column name for each

我有一些原始數據可以從()括起來的列表數組中獲取

[('2018-10-13T21:00:00.000000000', 71.457, 72.675, 68.45 , 69.252, 71.51 , 72.725, 68.505, 69.31 , 507708)
 ('2018-10-20T21:00:00.000000000', 69.252, 69.806, 65.72 , 67.685, 69.31 , 69.855, 65.77 , 67.74 , 389174)
 ('2018-10-27T21:00:00.000000000', 67.685, 67.924, 62.61 , 62.855, 67.74 , 67.975, 62.665, 62.905, 454709)
 ('2018-11-03T21:00:00.000000000', 62.855, 64.115, 59.244, 59.815, 62.905, 64.165, 59.295, 59.87 , 858696)
 ('2018-11-10T22:00:00.000000000', 59.815, 61.262, 54.732, 56.125, 59.87 , 61.315, 54.787, 56.175, 440074)]

我想將其作為pandas數據框並添加列名,使用for循環可通過所需的輸出來實現,但是如何使用內置資源的pandas直接在沒有for循環的情況下執行此操作以及如何將其存儲在pandas對象中。

for row in history:
    print("{0:s}, {1:,.5f}, {2:,.5f}, {3:,.5f}, {4:,.5f}, {5:d}".format(
    pd.to_datetime(str(row['Date'])).strftime(date_format), row['BidOpen'], row['BidHigh'],row['BidLow'], row['BidClose'], row['Volume']))

輸出:這里T在刪除的日期和時間之間浮動,十進制也要小心。如果沒有其他解決方案,如何將其存儲在pandas對象中。

Date, BidOpen, BidHigh, BidLow, BidClose, Volume
13.10.2018 21:00:00, 71.45700, 72.67500, 68.45000, 69.25200, 507708
20.10.2018 21:00:00, 69.25200, 69.80600, 65.72000, 67.68500, 389174
27.10.2018 21:00:00, 67.68500, 67.92400, 62.61000, 62.85500, 454709
03.11.2018 21:00:00, 62.85500, 64.11500, 59.24400, 59.81500, 858696
10.11.2018 22:00:00, 59.81500, 61.26200, 54.73200, 56.12500, 440074

這給出了正確的輸出

df = pd.DataFrame(history, columns=['Date', 'BidOpen', 'BidHigh','BidLow', 'BidClose', 'AskOpen', 'AskHigh', 'AskLow', 'AskClose', 'Volume'])

在這里,列名可以是任何名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM