- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在关注这里的答案:
Pandas: how to plot yearly data on top of each other
它采用时间序列并在新图上绘制每一天的最后一个数据点。图中的每条线代表一周的数据(例如每周 5 个数据点):
我使用了下面的代码来做到这一点:
#Chart by last price
daily = ts.groupby(lambda x: x.isocalendar()[1:]).agg(lambda s: s[-1])
daily.index = pd.MultiIndex.from_tuples(daily.index, names=['W', 'D'])
dofw = "Mon Tue Wed Thu Fri Sat Sun".split()
grid = daily.unstack('D').rename(columns=lambda x: dofw[x-1])
grid[-5:].T.plot()
我想做的不是按一天中的最后一个数据点进行汇总,而是按小时进行汇总(因此对每小时的数据进行平均)并绘制每周的每小时数据图表。因此图表看起来与链接图像中的图表相似,只是每行每天有 24 个数据点,而不是每行每天只有一个数据点
有什么方法可以将 Pandas DataFrame 粘贴到这篇文章中吗?当我单击复制粘贴时,它会粘贴为列表
编辑:
最终代码考虑了最近一周的不完整数据以用于制图:
# First we read the DataFrame and resample it to get a mean on every hour
df = pd.read_csv(r"MYFILE.csv", header=None,
parse_dates=[0], index_col=0).resample('H', how='mean').dropna()
# Then we add a week field so we can filter it by the week
df['week']= df.index.map(lambda x: x.isocalendar()[1])
start_range = list(set(df['week']))[-3]
end_range = list(set(df['week']))[-1]
# Create week labels
weekdays = 'Mon Tue Wed Thu Fri Sat Sun'.split()
# Create the figure
fig, ax = plt.subplots()
# For every week we want to plot
for week in range(start_range,end_range+1):
# Select out the week
dfw = df[df['week'] == week].copy()
# Here we align all the weeks to span over the same time period so they
# can be shown on the graph one over the other, and not one next to
# the other.
dfw['timestamp'] = dfw.index.values - (week * np.timedelta64(1, 'W'))
dfw = dfw.set_index(['timestamp'])
# Then we plot our data
ax.plot(dfw.index, dfw[1], label='week %s' % week)
# Now to set the x labels. First we resample the timestamp to have
# a date frequency, and set it to be the xtick values
if week == end_range:
resampled = resampled.index + pd.DateOffset(weeks=1)
else:
resampled = dfw.resample('D')
# newresampled = resampled.index + pd.DateOffset(weeks=1)
ax.set_xticks(resampled.index.values)
# But change the xtick labels to be the weekdays.
ax.set_xticklabels(weekdays)
# Plot the legend
plt.legend()
最佳答案
解决方案在代码中解释。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# First we read the DataFrame and resample it to get a mean on every hour
df = pd.read_csv('trayport.csv', header=None,
parse_dates=[0], index_col=0).resample('H', how='mean').dropna()
# Then we add a week field so we can filter it by the week
df['week']= df.index.map(lambda x: x.isocalendar()[1])
# Create week labels
weekdays = 'Mon Tue Wed Thu Fri Sat Sun'.split()
# Create the figure
fig, ax = plt.subplots()
# For every week we want to plot
for week in range(1, 4):
# Select out the week
dfw = df[df['week'] == week].copy()
# Here we align all the weeks to span over the same time period so they
# can be shown on the graph one over the other, and not one next to
# the other.
dfw['timestamp'] = dfw.index.values - (week * np.timedelta64(1, 'W'))
dfw = dfw.set_index(['timestamp'])
# Then we plot our data
ax.plot(dfw.index, dfw[1], label='week %s' % week)
# Now to set the x labels. First we resample the timestamp to have
# a date frequency, and set it to be the xtick values
resampled = dfw.resample('D')
ax.set_xticks(resampled.index.values)
# But change the xtick labels to be the weekdays.
ax.set_xticklabels(weekdays)
# Plot the legend
plt.legend()
结果如下:
关于python - Pandas 滴答数据按小时平均并绘制历史的每一周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18250353/
moment.js 库很棒,我几乎一直都在使用它,但我最近遇到了一些有趣的事情。我正在尝试按一年中的一周绘制数据,其中一个数据点是 2013 年 12 月 31 日,moment.js 告诉我这是第
有没有办法可以找到给定一年的最后一周的数字。 例如,对于年份 2018 , 2018 的最后一周数是 52。 年份 2015 , 上周数字是 53。 最佳答案 如果您指的是 ISO 周,那么您可以使用
我正在尝试创建一个 if 语句,该语句根据当前日期检查日期,如果日期少于 2 周,则抛出 错误。 我当前的代码 const moment = require('moment') const today
转换 Spark 数据帧 +----+---------+------+ |name|date |amount| +----+---------+------+ |Jhon|4/6/2018
我有这段代码,我想在从日期选择器获取的日期中添加 40 周,并在将 40 周(280 天)添加到从日期选择器获取的日期后获取新日期。 代码: public class MainActivity ext
我的 table 上有一个 timestamp字段,这是一个标准的 RethinkDB 日期字段。 我可以使用此时间戳字段按天/周/月对行进行分组吗?在 group() 文档中找不到任何示例。 最佳答
当用户单击日/周/月按钮时,我需要运行一些 Javascript 代码来重新加载日历。是否有类似 dayButtonClicked() 之类的回调? 发生BUG: 当我第一次加载日历时。最初的 Vie
当我收到年份、周数和星期几时,如何在 C# 中计算日期。例如:年份 = 2011周 = 27天 = 6 结果应该是 2011-7-10 感谢大家。我根据维基百科算法解决了它。 最佳答案 此处没有 C#
如何使用 c#.net 每天、每周和每月发送电子邮件? 我正在考虑创建一个 Windows 服务应用程序,但我不知道该怎么做,也不知道该怎么做。 非常感谢您的想法。 最佳答案 Windows 服务可能
最近我在处理全日历。我想更改周 View 中日期的格式。我发现很多人使用 columnFormat: { month: 'ddd', week: 'ddd d/M', day: 'dddd d/M'
我正在使用一个完整的日历插件来显示各种事件。 我的问题是,当我更改正在查看的月份/周,然后刷新时,我会回到当前的月份/周,而我想留在之前查看的同一个月/周。即如果现在是八月,我回到七月,然后刷新,我希
我有一个按周计算所有工单的查询,但我需要将其转换为动态两行报告,将周开始日期移动到列中? 这就是我试图让它做的事情.. 这是我的查询: SELECT td_type, FROM_DAYS(TO_DAY
我想获取某件事的统计数据。 我正在尝试统计今天、本周、本月的情况。 我的查询: "SELECT COUNT(id) FROM images i WHERE i.user_id = 3 GROUP BY
我正在开发一个有 200.000 个页面的网站。还有一个浏览部分,显示最受欢迎、评价最高等文档。然而,在发布几周后,此部分将变得几乎静态。所以我还想实现一个过滤系统,它将显示今天、本周、本月最受欢迎的
如何从字段日期早于现在 + 2 周的 MySQL 数据库中获取行? 我试过了 WHERE date_ready < DATE_SUB(CURDATE(), INTERVAL 2 WEEK) 但这不是我
我有下表: create table my_table ( SubjectID int, Date Date, Test_Value int ); insert into my_table(Su
好吧,这几天我一直在努力解决这个问题。我是一个 super 初学者,我有一个标签。在该标签中,我显示了日期 1/20/21。我能够做到这一点。然后我有一个按钮。我希望每次单击该按钮都会将日期增加 14
我有一个数据集,其中包含工厂 worker 三年产出的数据。现在我想获得基于日期、周、月的平均输出。问题是日期格式类似于 %d.%m.%Y(日-月-年)。我的问题是如何在保持日期格式不变的同时获得预期
我有这样的东西 DateTime.Now.ToString("dd.MM.yy"); 在我的代码中,我需要添加 1 周,比如 5.4.2012 变成12.4.2012 我试过转成int再累加,但是到3
我已经阅读了几篇关于在 mysql 查询中估计移动平均值的文章,但是我的情况似乎稍微困难一些,因为该表不包含我想要计算平均值的列。我需要计算每组的行数,并显示该组的移动平均值。 表中基本上只有一列相关
我是一名优秀的程序员,十分优秀!