1

I have a data structure like this:

data = [{
  "name": "leopard",
  "character": "mean",
  "skills": ["sprinting", "hiding"],
  "pattern": "striped",
 },
 {
  "name": "antilope",
  "character": "good",
  "skills": ["running"],
 },
 .
 .
 .
]

Each key in the dictionaries has values of type integer, string or list of strings (not all keys are in all dicts present), each dictionary represents a row in a table; all rows are given as the list of dictionaries.

How can I easily import this into Pandas? I tried

df = pd.DataFrame.from_records(data)

but here I get an "ValueError: arrays must all be same length" error.

halloleo
  • 9,216
  • 13
  • 64
  • 122

1 Answers1

0

The DataFrame constructor takes row-based arrays (amoungst other structures) as data input. Therefore the following works:

data = [{
  "name": "leopard",
  "character": "mean",
  "skills": ["sprinting", "hiding"],
  "pattern": "striped",
 },
 {
  "name": "antilope",
  "character": "good",
  "skills": ["running"],
 }]
df = pd.DataFrame(data)
print(df)

Output:

  character      name  pattern               skills
0      mean   leopard  striped  [sprinting, hiding]
1      good  antilope      NaN            [running]
halloleo
  • 9,216
  • 13
  • 64
  • 122
Nihal
  • 5,262
  • 7
  • 23
  • 41