Pandas groupby date month and count items within months

Question:

I have a dataframe like this:

STYLE | INVOICE_DATE2
A     | 2017-01-03
B     | 2017-01-03
C     | 2017-01-03
A     | 2017-02-03
A     | 2017-01-03
B     | 2017-02-03
B     | 2017-01-03

I’m trying to group them by month and count itself within month, result must like this:

Month | Item | Count
1     | A    | 2
      | B    | 2
      | C    | 1
2     | A    | 1
      | B    | 1

I have tried this:

lastyear_df.groupby([(df['INVOICE_DATE2']).dt.month, df['STYLE']])['STYLE'].count()

But it didn’t work for me.

Asked By: yigitozmen

||

Answers:

I think you are close, need size if want count NaNs:

d = {'INVOICE_DATE2':'Month','STYLE':'Item'}
df = (df.groupby([df['INVOICE_DATE2'].dt.month, 'STYLE'])
       .size()
       .reset_index(name='Count')
       .rename(columns=d))
print (df)
   Month Item  Count
0      1    A      2
1      1    B      2
2      1    C      1
3      2    A      1
4      2    B      1

Or count for count only no NaNs:

d = {'INVOICE_DATE2':'Month','STYLE':'Item'}
df = (df.groupby([df['INVOICE_DATE2'].dt.month, 'STYLE'])['STYLE']
       .count()
       .reset_index(name='Count')
       .rename(columns=d))
print (df)
   Month Item  Count
0      1    A      2
1      1    B      2
2      1    C      1
3      2    A      1
4      2    B      1

Last if need only one unique value in first column:

df['Month'] = df['Month'].mask(df.duplicated('Month'),'')
print (df)
  Month Item  Count
0     1    A      2
1          B      2
2          C      1
3     2    A      1
4          B      1
Answered By: jezrael

Here is a one liner…

ans = df.groupby([df.INVOICE_DATE2.apply(lambda x: x.month), 'STYLE']).count()

Here is the output

In [21]: ans
Out[21]:
                     INVOICE_DATE2
INVOICE_DATE2 STYLE
1             A                  2
              B                  2
              C                  1
2             A                  1
              B                  1

NOTE: That at this point you have a hierarchical index, which you can flatten by using reset_index

ans = ans.reset_index(1)
              STYLE  INVOICE_DATE2
INVOICE_DATE2
1                 A              2
1                 B              2
1                 C              1
2                 A              1
2                 B              1

You can now change the column and index names if you like:

ans.index.name = 'MONTH'
ans.columns = ['ITEM', 'COUNT']
Answered By: aquil.abdullah
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.