python - 在每个期间的列中重新组织 pandas 定时日期

标签 python pandas

我有一个大数据集,记录了多年来每十分钟的定时太阳能电池板产量日志。我想要一个条形图,显示这些日志每年的月度总和。

我不知道这是否有意义,所以让我用一个例子来解释一下。

通过重采样,我得到了以下数据框:

Last updated
2017-01-31 00:00:00+01:00     24.1
2017-02-28 00:00:00+01:00     44.8
2017-03-31 00:00:00+02:00    140.1
2017-04-30 00:00:00+02:00    168.7
2017-05-31 00:00:00+02:00    194.4
2017-06-30 00:00:00+02:00    214.7
2017-07-31 00:00:00+02:00    204.6
2017-08-31 00:00:00+02:00    159.5
2017-09-30 00:00:00+02:00    117.3
2017-10-31 00:00:00+01:00     74.8
2017-11-30 00:00:00+01:00     30.4
2017-12-31 00:00:00+01:00      7.2
2018-01-31 00:00:00+01:00      9.4
Freq: M, Name: Yield (kWh), dtype: float64

我认为实现我想要的绘图的最佳方法是以某种方式将其转换为数据框

  • 仅包含月份的索引(这对于 DateTimeIndex 是否可行,或者我应该简单地使用从 1 到 12 的 RangeIndex 吗?)
  • 每年一列(本例中为 2016 年和 2017 年)
  • 相应地重新组织值

例如:

Last updated   2017  2018
Jan            24.1   9.4
Feb            44.8     0
Mar           140.1     0
Apr           168.7     0
May           194.4     0
Jun           214.7     0
Jul           204.6     0
Aug           159.5     0
Sep           117.3     0
Oct            74.8     0
Nov            30.4     0
Dec             7.2     0

我可以通过一些天真的循环轻松实现这一点,但我相信 pandas 一定向我隐藏了一些聪明的技巧。有人能给我指出一个有效的解决方案吗?

非常感谢!

最佳答案

您可以使用pivot :

df = pd.pivot(index=df.index.strftime('%b'), 
              columns=df.index.year, 
              values=df['Last updated']).fillna(0).reindex(cats)
print (df)
      2017  2018
Jan   24.1   9.4
Feb   44.8   0.0
Mar  140.1   0.0
Apr  168.7   0.0
May  194.4   0.0
Jun  214.7   0.0
Jul  204.6   0.0
Aug  159.5   0.0
Sep  117.3   0.0
Oct   74.8   0.0
Nov   30.4   0.0
Dec    7.2   0.0

如果需要,另一个更通用的解决方案将Last update中的值与 groupby 进行聚合。按 strftime 生成的月份和 year ,然后聚合sum(或必要时mean),通过 unstack reshape 最后reindex :

df = (df.groupby([df.index.strftime('%b'), df.index.year])['Last updated']
        .sum().unstack(fill_value=0)
        .reindex(cats))
print (df)
      2017  2018
Jan   24.1   9.4
Feb   44.8   0.0
Mar  140.1   0.0
Apr  168.7   0.0
May  194.4   0.0
Jun  214.7   0.0
Jul  204.6   0.0
Aug  159.5   0.0
Sep  117.3   0.0
Oct   74.8   0.0
Nov   30.4   0.0
Dec    7.2   0.0

解决方案 ordered categorical s 正确的排序:

cats = ['Jan', 'Feb', 'Mar', 'Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
cat_type = pd.api.types.CategoricalDtype(categories=cats, ordered=True)
c = pd.Categorical(df.index.strftime('%b'), dtype=cat_type)

df = (df.groupby([c, df.index.year])['Last updated']
        .sum().unstack(fill_value=0))
print (df)
      2017  2018
Jan   24.1   9.4
Feb   44.8   0.0
Mar  140.1   0.0
Apr  168.7   0.0
May  194.4   0.0
Jun  214.7   0.0
Jul  204.6   0.0
Aug  159.5   0.0
Sep  117.3   0.0
Oct   74.8   0.0
Nov   30.4   0.0
Dec    7.2   0.0

关于python - 在每个期间的列中重新组织 pandas 定时日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48384422/

相关文章:

python - 使用 Python 将 XML 转为 MYSQL

python - Pandas 仅使用该 key 的一部分访问组元组 key 的长度

python - 神秘的Python Pandas lambda函数错误

python - KDB+ 像 asof 一样加入 pandas 中的时间序列数据?

python - 使用 Pandas 错位 header 的 CSV 到 Excel

python - 为什么这些点在Python中不相等?

python - 附加维度 numpy 数组

python - 如何更改 pandas 数据框中的文本片段

python - Tensorflow 没有重新识别一个热编码标签

python - 尝试使用经度和纬度获取距离,但一直运行到错误 : 'Series' object has no attribute 'radians'