【问题标题】:Pandas timestamp熊猫时间戳
【发布时间】:2018-09-20 08:41:09
【问题描述】:

我想对每天的数据进行分组并计算情绪的每日平均值。

我的 pandas 数据框有问题,因为我无法将 dateamp 中的日期列转换为使用 groupby() 函数。这是我的数据样本:

   sentiment              date
0  1  2018-01-01 07:37:07+00:00
1  0  2018-02-12 06:57:27+00:00
2  -1  2018-09-18 06:23:07+00:00
3  1 2018-09-18 07:23:10+00:00
4  0  2018-02-12 06:21:08+00:00

【问题讨论】:

  • print (df.info()) 是什么?
  • RangeIndex:5 个条目,0 到 2 个数据列(共 2 列):情感 5 个非空 int64 日期 5 个非空对象 dtypes: int64(1), object(1) 内存使用量:76.0+ 字节

标签: python-3.x pandas timestamp


【解决方案1】:

我认为需要resample - 它创建完整的DatatimeIndex

df['date'] = pd.to_datetime(df['date'])

df1 = df.resample('D',on='date')['sentiment'].mean()
#if want remove NaNs rows
df1 = df.resample('D',on='date')['sentiment'].mean().dropna()

groupby 和聚合meandates 或floor 删除时间:

df2 = df.groupby(df['date'].dt.date)['sentiment'].mean()
#DatetimeIndex in output
df2 = df.groupby(df['date'].dt.floor('d'))['sentiment'].mean()

【讨论】:

    猜你喜欢
    • 2019-06-08
    • 2020-12-06
    • 2013-02-03
    • 1970-01-01
    • 2017-02-23
    • 2020-04-16
    • 1970-01-01
    • 2014-10-28
    • 2019-06-16
    相关资源
    最近更新 更多