4

Can anyone suggest me an efficient way to convert list of list of dictionaries as pandas dataframe?

Input = [[{'name':'tom','roll_no':1234,'gender':'male'},
          {'name':'sam','roll_no':1212,'gender':'male'}],
          [{'name':'kavi','roll_no':1235,'gender':'female'},
          {'name':'maha','roll_no':1211,'gender':'female'}]]

The dictionary keys are same in the sample input provided and an expected output is,

Output =      name       roll_no       gender
          0   tom        1234          male
          1   sam        1212          male
          2   kavi       1235          female
          3   maha       1211          female
cs95
  • 379,657
  • 97
  • 704
  • 746
Mahamutha M
  • 1,235
  • 1
  • 8
  • 24
  • Please see the linked duplicate, especially [my answer](https://stackoverflow.com/a/53831756/4909087) to that question. – cs95 Dec 29 '18 at 17:10
  • The section "Summarising" in your answer has covered the nested dictionaries in a list, but not what I have asked i.e., nested list inside list with dictionaries. – Mahamutha M Dec 29 '18 at 17:19
  • Oh, ummmm, just flatten the lists; `from itertools import chain; pd.DataFrame(list(chain.from_iterable(Input)))` it is quite simple. – cs95 Dec 29 '18 at 17:22
  • Thanks for your answer. Is that mention in your answer in [https://stackoverflow.com/questions/20638006/convert-list-of-dictionaries-to-a-pandas-dataframe/53831756#53831756] ? – Mahamutha M Dec 29 '18 at 17:25
  • 1
    You are right, it is not. Will reopen. – cs95 Dec 29 '18 at 17:29

1 Answers1

4

You will need to flatten your input using itertools.chain, and you can then call the pd.DataFrame constructor.

from itertools import chain
pd.DataFrame(list(chain.from_iterable(data)))

   gender  name  roll_no
0    male   tom     1234
1    male   sam     1212
2  female  kavi     1235
3  female  maha     1211
cs95
  • 379,657
  • 97
  • 704
  • 746