如何将数据帧转换为一维字典?



我有一个像

这样的数据框架
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

我想把上面的数据框转换成每一行的数组字典,如

[
{"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
]

我正在研究情感分析,并希望将数组发送到html文件以表数据的形式打印。为此,我希望将数据帧转换为每一行的字典数组。

使用df.to_dict('records'),它将为您提供行字典。通过遍历元素,可以很容易地将字典的字典转换为字典的列表。

最新更新