python - 按月分组并绘制一个堆积在 pandas 中的条形图

标签 python numpy matplotlib pandas

我想要一个数据框,其中数据代表不同的类别以及每个类别的每月频率。例如,在下面的数据框中,想要使用 Forma 列来获取表示 Forma 列中每个类别的每月频率的数据框,例如获取一个数据框 df1

df    
                       Evento  Forma  Excentricidad
Fecha                                      
2004-04-09 22:45:00       1  MBCCM            0.7
2004-04-12 22:45:00       2  MBSCL            0.6
2004-04-24 03:45:00       3    SCL            0.4
2004-05-02 06:45:00       4    SCL            0.5
2004-05-30 04:45:00       5  MBCCM            0.9
2004-05-31 03:15:00       6  MBCCM            0.8
2004-06-08 00:15:00       7  MBSCL            0.6
2004-06-12 22:15:00       8    CCM            1.0
2004-06-13 02:45:00       9  MBCCM            0.8
2004-06-13 23:45:00      10  MBSCL            0.6
2004-06-14 03:15:00      11  MBSCL            0.6
2004-06-17 08:15:00      12  MBCCM            0.7
2004-06-17 11:45:00      13  MBCCM            0.7
2004-06-22 00:15:00      14    SCL            0.5
2004-06-22 07:45:00      15  MBCCM            0.9
2004-06-22 22:45:00      16    CCM            0.8 
2004-07-01 05:15:00      17  MBCCM            0.8
2004-07-02 00:15:00      18  MBSCL            0.6
2004-07-04 11:45:00      19  MBCCM            0.9
2004-07-06 03:45:00      20    SCL            0.6
2004-07-07 04:15:00      21    CCM            0.9
2004-07-08 02:45:00      22  MBCCM            1.0
2004-07-08 11:45:00      23  MBCCM            0.8
2004-07-08 02:15:00      24  MBCCM            0.9
2004-07-09 04:45:00      25    CCM            0.7
2004-07-11 18:15:00      26  MBSCL            0.4
2004-07-11 23:15:00      27  MBSCL            0.3
2004-07-15 10:45:00      28    CCM            0.8
2004-07-16 12:15:00      29  MBCCM            0.8
2004-07-17 02:15:00      30  MBCCM            0.8
2004-07-17 05:45:00      31  MBCCM            0.7
2004-07-19 23:15:00      32    CCM            0.9
2004-07-20 09:15:00      33    CCM            0.7
2004-07-20 21:45:00      34    SCL            0.6
2004-07-23 03:45:00      35    SCL            0.6
2004-07-23 12:45:00      36  MBCCM            0.9
2004-07-24 00:45:00      37    CCM            0.7
2004-07-26 00:15:00      38  MBCCM            0.8
2004-07-27 05:15:00      39  MBSCL            0.6
2004-07-27 07:15:00      40  MBSCL            0.6
2004-07-27 14:15:00      41  MBCCM            0.7
2004-07-27 19:45:00      42    SCL            0.6
2004-07-27 23:15:00      43  MBSCL            0.6
2004-07-28 07:15:00      44  MBCCM            0.8
2004-07-30 05:15:00      45  MBCCM            0.7
2004-07-31 00:15:00      46    SCL            0.5
2004-07-31 04:15:00      47  MBSCL            0.6

df1

Tipo    Abril  Mayo Junio Julio Agosto Septiembre Octubre
 MCC      2     9     8    1       5        6        3
 CCM      7     11    23   12      7        2        4
 MBCCM    4     8     4    1       3        4        2
 SCL      1      7    2     4      1        9        5
 MBSCL    6      3    7     1      9        3        10

我该如何从 df 做到这一点?

最佳答案

import pandas as pd
df = pd.read_table('data', sep='\s{2,}')
df.index = pd.to_datetime(df.index)
df['Month'] = [date.strftime('%B') for date in df.index]
print(pd.crosstab(rows=[df['Forma']], cols=[df['Month']], margins=False))

产量

Month  April  July  June  May
Forma                        
CCM        0     6     2    0
MBCCM      1    13     4    2
MBSCL      1     7     3    0
SCL        1     5     1    1

关于python - 按月分组并绘制一个堆积在 pandas 中的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448967/

相关文章:

python - 通过 pyfmi.load_fmu 加载 fmu 时出错

python - 我该如何着手定制夹层墨盒商店/产品?

Python:循环/扫描对象中的字段

python - 如何使用 SciKit 随机森林的 oob_decision_function_ 学习曲线?

python - 当值超出范围时,在二维数组中显示错误的轴

python - 如何在 tkinter GUI 中删除外部 Matplot 框架

python - 使用 scipy/numpy 在 python 中对数据进行分箱

python-3.x - 当我尝试将数据帧输出到 csv 文件时,为什么我的输出只有一行?Python3/boto3

python - Matplotlib 将带有 plot 和 pcolor(mesh) 的子图混合在一起,以便在 colorbar 打开时具有相同的 x 轴长度

python - MatPlotLib - 子图的子图或单个图上的多个断轴图