2

Trying to construct pandas DataFrame from list of dicts

List of dicts:

a = [{'1': 'A'},
{'2': 'B'},
{'3': 'C'}]

Pass list of dicts into pd.DataFrame():

df = pd.DataFrame(a)

Actual results:

    1   2   3
0   A   NaN NaN
1   NaN B   NaN
2   NaN NaN C
pd.DataFrame(a, columns=['Key', 'Value'])

Actual results:

    Key     Value
0   NaN     NaN
1   NaN     NaN
2   NaN     NaN

Expected results:

    Key Value   
0   1   A
1   2   B   
2   3   C
yongsheng
  • 376
  • 3
  • 19

2 Answers2

1

Something like this with a list comprehension:

pd.DataFrame(([(x, y) for i in a for x, y in i.items()]),columns=['Key','Value'])

  Key Value
0   1     A
1   2     B
2   3     C
anky
  • 74,114
  • 11
  • 41
  • 70
  • If I may learn more from your answer, why do I need to pass in a list of key, value pairs from the dicts as tuples to DataFrame to get this to work ? (specifically - why do I need x, y as tuples) – yongsheng Jul 31 '19 at 10:19
  • 1
    @yongsheng either a single dictionary or a list of iterables are accepted by the [Dataframe constructor](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html) – anky Jul 31 '19 at 10:51
1

try this,

from collections import ChainMap
data = dict(ChainMap(*a))
pd.DataFrame(data.items(), columns= ['Key','Value'])

O/P:

  Key Value
0   1     A
1   2     B
2   3     C
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
  • Interesting answer.. thank you sir. May I ask why this reverses the order of the keys to 3, 2, 1 ? – yongsheng Jul 31 '19 at 10:30
  • 1
    because you are constructing dataframe from dictionary. Dictionary is onordered data structure i.e., you can't expect same order in dictionary. If you need it as order you have to use ordereddict, for more details https://stackoverflow.com/questions/1867861/how-to-keep-keys-values-in-same-order-as-declared – Mohamed Thasin ah Jul 31 '19 at 10:33