如何优化我的Python数组到Pandas DataFrame转换?

时间:2018-04-24 13:03:01

标签: python arrays pandas dataframe

我有一个Python数组,我想将其转换为Pandas DataFrame。我为它写了一些代码,但速度很慢。 关于如何让它更快的任何建议?

这就是Python数组的内容通常如下所示:

[[1523937720000, '0.01000000', '0.01000000', '0.01000000', '0.01000000', '2278.69000000', 1523937779999, '22.78690000', 2, '346.19000000', '3.46190000', '0'], [1523937780000, '0.01000000', '0.02500000', '0.01000000', '0.01404000', '838.33000000', 1523937839999, '12.48726080', 6, '100.00000000', '2.50000000', '0']]

我为它创建了一个DataFrame:

pandaresult = pd.DataFrame(columns=['open_time','open','close','high','low','volume','close_time','quote_volume','trades','tbbav','tbqav']).astype({'open_time':'datetime64[ms]','close':'float_', 'high':'float_','low':'float_','volume':'float_','close_time':'datetime64[ms]','quote_volume':'float_','trades':'float_','tbbav':'float_','tbqav':'float_'})

这是填充DataFrame的代码:

for index, line in enumerate(result):
    pandaresult = pandaresult.append({'open_time':(pd.to_datetime(line[0], unit='ms')),'open':float(line[1]),'close':float(line[2]),'high':float(line[3]),'low':float(line[4]),'volume':float(line[5]),'close_time':(pd.to_datetime(line[6], unit='ms')),'quote_volume':float(line[7]),'trades':float(line[8]),'tbbav':float(line[9]),'tbqav':float(line[10])}, ignore_index=True)

我可以做些什么来加快速度?

0 个答案:

没有答案