计算每月发生的次数

时间:2019-07-05 13:21:51

标签: pandas

我有一个Pandas数据框,希望每月统计和显示每月发生的事件,如下所示。我该怎么办?

我有一个Pandas数据框,如下所示:

Name    Month

Jack    February
Jill    December
Bill    September
Jack    June
Jill    August
Bill    July
.
.
.

我希望统计每个月出生的名字,然后显示如下:

Name    January   February   March  ...  December

Jack      0          6        10    ...     3
Jill      2          8         4    ...     2
Bill      4          5         9    ...     0
.
.
. 

我该怎么做?

2 个答案:

答案 0 :(得分:0)

可用数据

   Name      Month
0  Jack   February
1  Jill   December
2  Bill  September
3  Jack       June
4  Jill     August
5  Bill       July
6  Mark       July
7  Mark       July


使用pd.get_dummies

pd.get_dummies(df.set_index('Name')['Month']).reset_index().groupby('Name').sum()

输出

      August  December  February  July  June  September
Name                                                   
Bill       0         0         0     1     0          1
Jack       0         0         1     0     1          0
Jill       1         1         0     0     0          0
Mark       0         0         0     2     0          0

答案 1 :(得分:0)

您可以在此处使用pandas.crosstab

months = ['January', 'February', 'March', 'April', 'May', 'June',
          'July', 'August', 'September', 'October', 'November', 'December']

pd.crosstab(df.Name, df.Month).reindex(months, axis=1, fill_value=0)

[出]

Month  January  February  March  April  May  June  July  August  September  \
Name                                                                         
Bill         0         0      0      0    0     0     1       0          1   
Jack         0         1      0      0    0     1     0       0          0   
Jill         0         0      0      0    0     0     0       1          0   

Month  October  November  December  
Name                                
Bill         0         0         0  
Jack         0         0         0  
Jill         0         0         1