python - How to Split a comma separated string of dictionaries into a Pandas dataframe -


i have string in format:

{apple:"34253453",oranges:"sweet",x:"cool"},{apple:"34222453",oranges:"dry",x:"warm"},{apple:"31113453",oranges:"bitter",x:"hot"},{apple:"38883453",oranges:"sweet",x:"cool"} 

and looking create dataframe column labels 'apple','oranges','x' , values placed in respective rows.

i've tried use solution: python convert comma separated list pandas dataframe ast.literal_eval convert list before transforming dataframe no luck.

your string invalid json, necessary replace first:

import ast  s = '{apple:"34253453",oranges:"sweet",x:"cool"},{apple:"34222453",oranges:"dry",x:"warm"},{apple:"31113453",oranges:"bitter",x:"hot"},{apple:"38883453",oranges:"sweet",x:"cool"}'  ss = '[' + s.replace('{', '{"').replace(':"','":"').replace('",', '","') + ']' print (ss)  [{"apple":"34253453","oranges":"sweet","x":"cool"},  {"apple":"34222453","oranges":"dry","x":"warm"},  {"apple":"31113453","oranges":"bitter","x":"hot"},  {"apple":"38883453","oranges":"sweet","x":"cool"}] 

df = pd.dataframe(ast.literal_eval(ss)) print (df)       apple oranges     x 0  34253453   sweet  cool 1  34222453     dry  warm 2  31113453  bitter   hot 3  38883453   sweet  cool 

df = pd.dataframe(pd.io.json.loads(ss)) print (df)       apple oranges     x 0  34253453   sweet  cool 1  34222453     dry  warm 2  31113453  bitter   hot 3  38883453   sweet  cool 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -