具有Data_Time数据转换功能的Python Financial OHLC:将数组列表列出给Pandas并为每个列表附加列名

时间:2018-11-14 12:55:01

标签: python pandas list dataframe

我从某些来源获取了原始数据作为()括起来的列表数组

[('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

1 个答案:

答案 0 :(得分:0)

这将提供正确的输出

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

列名可以是任何名称。