简体   繁体   English

熊猫:如何将字符串转换为DataFrame

[英]Pandas: How to convert string to DataFrame

Hi I have the following data (string) and am struggling to convert it into a pandas dataframe. 嗨,我有以下数据(字符串),正在努力将其转换为熊猫数据框。

Any help would be greatly appreciated! 任何帮助将不胜感激!

pd.DataFrame with "," as the delim doesnt work given the commas else where in the data. pd.DataFrame中带有“,”的delim在给定逗号的情况下不起作用。

[["Time","Forecast"],["2019-07-08T23:00:00Z",20],["2019-07-08T23:30:00Z",26],["2019-07-09T00:00:00Z",24],["2019-07-09T00:30:00Z",26]]

IIUC, you can use ast.literal_eval : IIUC,您可以使用ast.literal_eval

s='[["Time","Forecast"],["2019-07-08T23:00:00Z",20],["2019-07-08T23:30:00Z",26],["2019-07-09T00:00:00Z",24],["2019-07-09T00:30:00Z",26]]'
l=ast.literal_eval(s) #convert to actual list of list
df=pd.DataFrame(l[1:],columns=l[0])

                   Time  Forecast
0  2019-07-08T23:00:00Z        20
1  2019-07-08T23:30:00Z        26
2  2019-07-09T00:00:00Z        24
3  2019-07-09T00:30:00Z        26
import pandas as pd
from collections import defaultdict 

lst = [["Time","Forecast"],["2019-07-08T23:00:00Z",20],["2019-07-08T23:30:00Z",26],["2019-07-09T00:00:00Z",24],["2019-07-09T00:30:00Z",26]]
map = defaultdict(list) 
keys = lst[0] 
for i, el in enumerate(lst): 
    if i != 0: 
        map[keys[0]].append(el[0]) 
        map[keys[1]].append(el[1]) 

pd.DataFrame(map)                                                                                                                                                                                                                                    

   Forecast                  Time
0        20  2019-07-08T23:00:00Z
1        26  2019-07-08T23:30:00Z
2        24  2019-07-09T00:00:00Z
3        26  2019-07-09T00:30:00Z

You can make a proper dictionary out of your data and make a df with it. 您可以根据数据制作适当的字典,并使用它进行df处理。

>>> import pandas as pd
>>> from collections import defaultdict
>>> data = [["Time","Forecast"],["2019-07-08T23:00:00Z",20],["2019-07-08T23:30:00Z",26],["2019-07-09T00:00:00Z",24],["2019-07-09T00:30:00Z",26]]
>>> columns = data[0]
>>> rows = data[1:]
>>> d = defaultdict(list)
>>> for item in rows:
...     d[columns[0]].append(item[0])
...     d[columns[1]].append(item[1])
...
>>> df = pd.DataFrame(d)
>>> df
                   Time  Forecast
0  2019-07-08T23:00:00Z        20
1  2019-07-08T23:30:00Z        26
2  2019-07-09T00:00:00Z        24
3  2019-07-09T00:30:00Z        26
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM