0

I am not entirely sure if this is possible but I thought I would go ahead and ask. I currently have a string that looks like the following:

myString = 
"{"Close":175.30,"DownTicks":122973,"DownVolume":18639140,"High":177.47,"Low":173.66,"Open":177.32,"Status":29,"TimeStamp":"\/Date(1521489600000)\/","TotalTicks":245246,"TotalVolume":33446771,"UnchangedTicks":0,"UnchangedVolume":0,"UpTicks":122273,"UpVolume":14807630,"OpenInterest":0}
 {"Close":175.24,"DownTicks":69071,"DownVolume":10806836,"High":176.80,"Low":174.94,"Open":175.24,"Status":536870941,"TimeStamp":"\/Date(1521576000000)\/","TotalTicks":135239,"TotalVolume":19649350,"UnchangedTicks":0,"UnchangedVolume":0,"UpTicks":66168,"UpVolume":8842514,"OpenInterest":0}"

The datasets can be varying lengths (this example has 2 datasets but it could have more), however the parameters will always be the same, (close, downticks, downvolume, etc).

Is there a way to create a dataframe from this string that takes the parameters as the index, and the numbers as the values in the column? So the dataframe would look something like this:

df = 


              0         1
index

Close        175.30    175.24
DownTicks    122973    69071
DownVolume   18639140  10806836
High         177.47    176.80
Low          173.66    174.94
Open         177.32    175.24

(etc)...    
3
  • is your string surrounded by single quotes? Commented Jul 18, 2018 at 13:58
  • yes, i actually got this string from a json dump which yields that result Commented Jul 18, 2018 at 13:59
  • aren't you missing a comma at the end of the first dictionary? Commented Jul 18, 2018 at 14:01

1 Answer 1

1

It looks like there are some issues with your input. As mentioned by @lmiguelvargasf, there's a missing comma at the end of the first dictionary. Additionally, there's a \n which you can simply use a str.replace to fix.

Once those issues have been solved, the process it pretty simple.

myString = '''{"Close":175.30,"DownTicks":122973,"DownVolume":18639140,"High":177.47,"Low":173.66,"Open":177.32,"Status":29,"TimeStamp":"\/Date(1521489600000)\/","TotalTicks":245246,"TotalVolume":33446771,"UnchangedTicks":0,"UnchangedVolume":0,"UpTicks":122273,"UpVolume":14807630,"OpenInterest":0}
 {"Close":175.24,"DownTicks":69071,"DownVolume":10806836,"High":176.80,"Low":174.94,"Open":175.24,"Status":536870941,"TimeStamp":"\/Date(1521576000000)\/","TotalTicks":135239,"TotalVolume":19649350,"UnchangedTicks":0,"UnchangedVolume":0,"UpTicks":66168,"UpVolume":8842514,"OpenInterest":0}'''
myString = myString.replace('\n', ',')
import ast
list_of_dicts = list(ast.literal_eval(myString))
df = pd.DataFrame.from_dict(list_of_dicts).T
df
                                       0                        1
Close                              175.3                   175.24
DownTicks                         122973                    69071
DownVolume                      18639140                 10806836
High                              177.47                    176.8
Low                               173.66                   174.94
Open                              177.32                   175.24
OpenInterest                           0                        0
Status                                29                536870941
TimeStamp        \/Date(1521489600000)\/  \/Date(1521576000000)\/
TotalTicks                        245246                   135239
TotalVolume                     33446771                 19649350
UnchangedTicks                         0                        0
UnchangedVolume                        0                        0
UpTicks                           122273                    66168
UpVolume                        14807630                  8842514
Sign up to request clarification or add additional context in comments.

2 Comments

@W Stokvis should there just be a comma after the first dictionary and not a newline?
Yes. The newline character was giving me an error when I tried to transform it using ast.literal_eval. You additionally will want to encapsulate myString in triple quotes since it's multiple lines. I'm editing my answer to account for that.

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.