nishant nishant - 10 months ago 25
Python Question

Converting a string with list of dicts to pandas df

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


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


try this:

In [73]: pd.DataFrame.from_dict([{'key1':'val1','key2':'val2'},{'key1':'val1','key2':'val2'}])
   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))
   key1  key2
0  val1  val2
1  val1  val2