按月分组并绘制堆放在熊猫中的酒吧

时间:2014-05-03 19:37:40

标签: python numpy matplotlib pandas

我想获得一个数据框,其中数据代表每个类的不同类和每月频率。例如,在以下数据框中,想要使用列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?

1 个答案:

答案 0 :(得分:4)

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