将带有字符串的嵌套列表转换为Pandas Dataframe

时间:2018-05-22 19:44:03

标签: python pandas dataframe

我有来自Redis的数据,我很难在DF中转换这些数据

Redis的数据

data = ["[Timestamp('2018-05-22 09:15:00'), 3555.75, 3559.15, 3546.45, 3548.3, 34250, 'Green', 34250]",
 "[Timestamp('2018-05-22 09:16:00'), 3549.05, 3551, 3543.25, 3548, 19500, 'Green', 53750]",
 "[Timestamp('2018-05-22 09:17:00'), 3548.95, 3553.2, 3548.05, 3548.9, 12000, 'Green', 65750]"]

如何将以上数据存储在以下列的pandas数据框中

df = pd.DataFrame(columns= 'date','open','high','close','low','volume','close','total_volume'])

2 个答案:

答案 0 :(得分:2)

正如我上面所说,这里最简单的方法是改变你向上游发送数据的方式。如果这不是一个选项,这是一种使用您当前数据的方法:

使用 split

strip

data = [i.strip('[]').split(',') for i in data]

<强> pd.DataFrame

df = pd.DataFrame(data, columns=['date','open','high','close','low','volume','close','total_volume'])

                               date      open      high     close      low  \
0  Timestamp('2018-05-22 09:15:00')   3555.75   3559.15   3546.45   3548.3
1  Timestamp('2018-05-22 09:16:00')   3549.05      3551   3543.25     3548
2  Timestamp('2018-05-22 09:17:00')   3548.95    3553.2   3548.05   3548.9

   volume     close total_volume
0   34250   'Green'        34250
1   19500   'Green'        53750
2   12000   'Green'        65750

如果您的Timestamp列始终具有上述格式,则可以使用基本字符串切片对其进行后处理:

pd.to_datetime(df.date.str[11:-2])

0   2018-05-22 09:15:00
1   2018-05-22 09:16:00
2   2018-05-22 09:17:00
Name: date, dtype: datetime64[ns]

答案 1 :(得分:0)

import pandas as pd
import numpy as np
import datetime

data = [[pd.Timestamp('2018-05-22 09:15:00'), 3555.75, 3559.15, 3546.45, 3548.3, 34250, 'Green', 34250],
[pd.Timestamp('2018-05-22 09:16:00'), 3549.05, 3551, 3543.25, 3548, 19500, 'Green', 53750],
[pd.Timestamp('2018-05-22 09:17:00'), 3548.95, 3553.2, 3548.05, 3548.9, 12000, 'Green', 65750]]


DataFrame = pd.DataFrame(data, columns=['date', 'open', 'high', 'close', 'low', 'volume', 'close', 'total_volume'])
print(DataFrame)

这是你的输出:

             date     open     high    close     low  volume  close  \
0 2018-05-22 09:15:00  3555.75  3559.15  3546.45  3548.3   34250  Green   
1 2018-05-22 09:16:00  3549.05  3551.00  3543.25  3548.0   19500  Green   
2 2018-05-22 09:17:00  3548.95  3553.20  3548.05  3548.9   12000  Green   

total_volume  
0         34250  
1         53750  
2         65750