Home > other >  DataFrame from list of string dicts with array() values
DataFrame from list of string dicts with array() values

Time:08-09

So I have a list where each entry looks something like this:

"{'A': array([1]), 'B': array([2]), 'C': array([3])}"

I am trying to get a dataframe that looks like this

    A   B   C
0   1   2   3
1   4   5   6 
2   7   8   9

But I'm having trouble converting the format into something that can be read into a DataFrame. I know that pandas should automatically convert dicts into dataframes, but since my list elements are surrounded by quotes, it's getting confused and giving me

               0
0  {'A': array([1), 'B': array([2]), 'C': array([3])}
...

I originally asked a question with an oversimplified my example dict as {'A': 1, 'B': 2, 'C': 3} so methods such as ast.literal_eval, and eval should typically work, but in the case of the arrays as values, I am running into a NameError NameError: name 'array' is not defined.

CodePudding user response:

Assuming those really are arrays of length 1, this hackery should do the job:

data = [
  "{'A': array([1]), 'B': array([2]), 'C': array([3])}",
  "{'A': array([4]), 'B': array([5]), 'C': array([6])}",
  "{'A': array([7]), 'B': array([8]), 'C': array([9])}"
]

import ast
import pandas as pd
data = [ast.literal_eval(d.replace('array([','').replace('])','')) for d in data]
a = pd.DataFrame(data)
print(a)

Output:

   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9
  • Related