python-3.x - Python/Pandas 按日期聚合

标签 python-3.x pandas matplotlib seaborn

我正在尝试计算并绘制每天每个区域的数据点数量,到目前为止我有: enter image description here

但我想显示每个县每天的实例数,最终目标是将它们绘制在折线图上,例如:

enter image description here

只是我想将每个县绘制在自己的线上,而不是我上面绘制的总数。

更新:

我已经设法从提供的答案中得到这个:

enter image description here

这太棒了,正是我想要的。然而,事后看来,这看起来有点困惑,而且即使对于绘制的短期时间段也不是很有描述性,更不用说如果我要绘制几年的数据了。

所以我想在 8 个网格图上单独绘制它。但是,当我尝试为一个县绘制此图时,我得到了 bool 值。如下:

enter image description here

仅绘制真实值的最佳方法是什么?

最佳答案

你可以试试

df.county.groupby([df.date_stamp, df.county]).count().unstack().plot();
  • df.county...count() 是您要绘制的数值系列。
  • groupby([df.date_stamp, df.county]) 首先按 date_stamp 分组,然后按 country 分组(顺序很重要)。
  • unstack 将创建一个 Dataframe,其索引是时间戳,列是县。
  • plot(); 将绘制它(并且 ; 抑制不必要的输出)。

编辑

要将其绘制在单独的图上,您可以执行类似的操作

for county in df.county.unique():
    this_county = df[df.county == county]
    this_county.county.groupby(df.date_stamp).count().plot();
    title(county);
    show();

关于python-3.x - Python/Pandas 按日期聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49493525/

相关文章:

python - 如何根据列值合并数据框中的行?

python - 格式化规范以显示零 (0) 的空白(空字符串)

python - 删除行不是 .isin ('X' )

python - 基于 R 平方值创建数据框

python - 颜色栏中的偏移刻度标签

python - 有没有办法从 dict 和 collections.abc.MutableMapping 一起子类化?

python - 如何在数学函数中使用获取下一个乘法数字

python - 使用 while 循环过滤 Pandas DataFrame

python - matplotlib/seaborn : first and last row cut in half of heatmap plot

python - 在 Julia 中使用 PyPlot 为动画实现迭代器