nishant nishant - 5 months ago 12
Python Question

Converting a string with list of dicts to pandas df

I am reading a string from a file which looks like:

"[{key1:val1,key2:val2},{key1:val1,key2:val2}.....}]"


How do I convert this string into a pandas dataframe with key1 and key2 as two columns?

I can think of string manipulations like first removing '[' and ']' characters and then splitting on '},{' , append '}' to each item which would give me a list of
"{key1:value1,key2:value2}"
strings and then performing operations on each item of the list.

But that will be a bit cumbersome. I was wondering if there's a cleaner way to do that.

Answer

try this:

In [73]: pd.DataFrame.from_dict([{'key1':'val1','key2':'val2'},{'key1':'val1','key2':'val2'}])
Out[73]:
   key1  key2
0  val1  val2
1  val1  val2

or from the JSON string:

import json

In [81]: s
Out[81]: '[{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}]'

In [82]: pd.DataFrame.from_dict(json.loads(s))
Out[82]:
   key1  key2
0  val1  val2
1  val1  val2