2

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.

[["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]]
1
  • looks as unserialized string from some pipeline, tried to work around the data source? Commented Aug 6, 2019 at 11:16

3 Answers 3

3

IIUC, you can use 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
Sign up to request clarification or add additional context in comments.

1 Comment

@joshsol Glad I could help :)
0
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

Comments

0

You can make a proper dictionary out of your data and make a df with it.

>>> 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
>>>

2 Comments

nice modification of my answer
I didn't notice your answer, I was with the editor open already, it's basically the same solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.