我有一些代码:
df = pd.DataFrame.from_dict({
'sentencess' : sentencess,
'publishedAts' : publishedAts,
'hasil_sentimens' : hasil_sentimens
})
df.to_csv('chart.csv')
df['publishedAts'] = pd.to_datetime(df['publishedAts'], errors='coerce')
by_day_sentiment = df.groupby([
pd.Grouper(key='publishedAts',freq='D'),
'hasil_sentimens'
]).size().unstack('hasil_sentimens')
sentiment_dict = by_day_sentiment.to_dict('dict')
sentiment_dict_new = {
k: {
m.strftime('%Y-%m-%d %H:%M:%S'): v if v == v else 0 for m, v in v.items()
} for k, v in sentiment_dict.items()}
filter = {k:list(v.values()) for k, v in sentiment_dict_new.items()}
过滤器的输出是:
{
'Negatif ': [4.0, 2.0, 3.0, 1.0],
'Netral ': [3.0, 1.0, 3.0, 1.0],
'Positif ': [0, 0, 1.0, 1.0],
'tanggal': [
'2019-08-27 00:00:00',
'2019-08-28 00:00:00',
'2019-08-29 00:00:00',
'2019-08-30 00:00:00'
]
}
如何从键求和值,所以,我希望输出是:
{'Negatif ': [10.0], 'Netral ': [9.0], 'Positif ': [2.0]}
最佳答案
filter = {k:sum(list(v.values())) for k, v in sentiment_dict_new.items() if k!= 'tanggal'}
关于python - 如何求和值,python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57729002/