Home > Mobile >  How do I convert a dataframe into a Dictionary of 1D
How do I convert a dataframe into a Dictionary of 1D

Time:06-28

I have a dataframe like

                                             Tweets  Subjectivity  Polarity Sentiment
0  This is a sad day. Reversing Roe v. Wade is an...      0.833333  0.000000    Nutral
1  Through renewed investments in malaria and NTD...      0.125000  0.000000    Nutral
2  Daniel Boakye of  has spent 45 years working o...      0.317857  0.092857  Positive
3  When most people think about what produces gre...      0.590000  0.173333  Positive
4  The Power by  raises timely questions about th...      0.000000  0.000000    Nutral

I want to convert the above dataframe into a array of dictionary of each row like

[
  {"Tweets" : "tweet1","Subjectivity" : "subjectivity","Polarity":"polarity","Sentiment":"sentiment"},

  {"Tweets" : "tweet2","Subjectivity" : "subjectivity","Polarity":"polarity","Sentiment":"sentiment"},

  {"Tweets" : "tweet3","Subjectivity" : "subjectivity","Polarity":"polarity","Sentiment":"sentiment"}
  
 ...and so on
]

I am working on sentiment analysis and want to send the array to the html file to print in form of table data . For this i want the dataframe to be converted into array of dictionary of each row.

CodePudding user response:

Use df.to_dict('records') which will give you a dictionary of the rows. You can easily convert the dictionary of dictionaries to a list of dictionaries by iterating through the elements.

  • Related