【问题标题】:How to calculate the rolling mean for particular columns and need to print rolling mean with along with date如何计算特定列的滚动平均值并需要打印滚动平均值以及日期
【发布时间】:2019-04-16 02:10:01
【问题描述】:

我有带有标签 app_name、日期和总收入列的数据框,计算了窗口 2 的总收入的滚动平均值。 我对此很陌生

 App_Name           Date        Gross Revenue
com.alpha.studio    2018-10-16   11643154
com.alpha.studio    2018-10-17   13198984
com.alpha.studio    2018-10-18   24217875

我写了这样的代码

rolling_mean = com_fivemobile_thescore['Gross Revenue'].astype(int).rolling(2).mean()
rolling_std = com_fivemobile_thescore['Gross Revenue'].astype(int).rolling(2).std()
print ("mean and std----------",rolling_mean)

得到这样的东西

65259     3352.5
231872    3245.5
226967    1936.0
162993    2583.0
237743    3190.5
228604    2550.5
219176    1698.0

预期格式是:我需要滚动平均日期

            Date        Gross Revenue
   1             NaN
   2018-10-16    3352.5
   2018-10-17    3245.5
   2018-10-18    2583.0

请帮忙解决这个问题

【问题讨论】:

  • 再说一次,为什么你的问题在一分钟内就获得了 3 个赞?您的上一个问题在 3 分钟内获得了 4 个赞成票。这在这个标签中是闻所未闻的。

标签: pandas dataframe time-series data-science


【解决方案1】:

您可以将日期设置为索引,然后计算移动平均值:

com_fivemobile_thescore.set_index('Date', inplace=True)
rolling_mean = com_fivemobile_thescore['Gross Revenue'].rolling(2).mean()

如果您不想将日期保留为最终格式的索引,您可以重置索引。

rolling_mean = rolling_mean.reset_index(drop=False)

【讨论】:

  • @K Zhao 当我尝试重置索引时出现以下错误引发 TypeError('Cannot reset_index inplace on a Series ' TypeError: Cannot reset_index inplace on a Series to create a DataFrame
  • 在这种情况下,您似乎不能使用“就地”。您可以创建一个新变量。我已经编辑了我的答案。
猜你喜欢
  • 2023-01-31
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 2018-05-08
  • 2023-02-08
  • 2022-01-14
相关资源
最近更新 更多