【问题标题】:Get count or sum of values in dataframe of each day获取每天数据框中的值的计数或总和
【发布时间】:2018-05-25 15:52:52
【问题描述】:

如何计算每个日期所包含的值之和(1)和值之和(0)?

如何计算每个日期的值之和(1)除以值之和(0)。

sentiment_value = log10(count_of_(1)/count_of_(0)),这是我使用的公式。

date    new_sentiment
0   2017-04-28  1.0
1   2017-04-28  1.0
2   2017-04-28  1.0
3   2017-04-27  0.0
4   2017-04-27  1.0
5   2017-04-26  0.0
6   2017-04-26  1.0
7   2017-04-26  1.0
8   2017-04-26  0.0
9   2017-04-26  1.0

result_neg = date_df.appl

【问题讨论】:

  • 你能复制粘贴图片的内容吗?

标签: python python-2.7 pandas sentiment-analysis


【解决方案1】:

你需要:

g = data.groupby(['date', 'new_sentiment']).size().unstack(fill_value=0).reset_index()
g['sentiment_value'] = np.log((g[1.0])/(g[0.0]))

输出:

new_sentiment   date    0.0 1.0 sentiment_value
0            2017-04-26 2   3   0.405465
1            2017-04-27 1   1   0.000000
2            2017-04-28 0   3   inf

【讨论】:

  • 为什么使用 inf 值?
  • @MuhFadlizi 那是因为2017-04-28,没有'0'。因此log(0) 变得未定义。或inf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
相关资源
最近更新 更多