1

I have dictionaries stored in the below format

0     {'neg': 0.013, 'neu': 0.783, 'pos': 0.205, 'co...
1     {'neg': 0.072, 'neu': 0.68, 'pos': 0.248, 'com...
2     {'neg': 0.017, 'neu': 0.721, 'pos': 0.262, 'co...
3     {'neg': 0.03, 'neu': 0.783, 'pos': 0.187, 'com...
4     {'neg': 0.029, 'neu': 0.683, 'pos': 0.288, 'co...

I want to convert these into a dataframe with keys (neg, neu, pos, com) as columns and the values as rows of each index:

neg   neu   pos   com
.013  .783  .205  .456
.072  .68   .248  .456
......................
......................

The pattern should be as above. I tried hard but was not able to figure out. Please help.

  • 3
    Does this answer your question? [Convert list of dictionaries to a pandas DataFrame](https://stackoverflow.com/questions/20638006/convert-list-of-dictionaries-to-a-pandas-dataframe) – ksha Dec 13 '20 at 05:07
  • Are the keys always the same in each row? – Tim Biegeleisen Dec 13 '20 at 05:07

2 Answers2

2
df = pd.DataFrame(dict_name)

You can also try another

pd.DataFrame.from_records(dict_name) 
mhhabib
  • 2,733
  • 1
  • 11
  • 22
0

You can use the pandas library to perform this task. But first you need to combine all your dictionaries into one dictionary by doing:

dict1.update(dict2)# do this for all dicts

After running the above line, dict1 and dict2 will be save in dict1.

After that, you can turn your final dict into a pandas data frame.

import pandas as pd
myDataFrame = pd.DataFrame.from_dict(dict1)
karim
  • 33
  • 6