Pandas: count some values in a column
<p>I have dataframe, it's part of them</p>

<pre><code>    ID,"url","app_name","used_at","active_seconds","device_connection","device_os","device_type","device_usage"     
e990fae0f48b7daf52619b5ccbec61bc,"",Phone,2015-05-01 09:29:11,13,3g,android,smartphone,home     
e990fae0f48b7daf52619b5ccbec61bc,"",Phone,2015-05-01 09:33:00,3,unknown,android,smartphone,home     
e990fae0f48b7daf52619b5ccbec61bc,"",Phone,2015-06-01 09:33:07,1,unknown,android,smartphone,home     
e990fae0f48b7daf52619b5ccbec61bc,"",Phone,2015-06-01 09:34:30,5,unknown,android,smartphone,home     
e990fae0f48b7daf52619b5ccbec61bc,"",Messaging,2015-06-01 09:36:22,133,3g,android,smartphone,home        
e990fae0f48b7daf52619b5ccbec61bc,"",Messaging,2015-05-02 09:38:40,5,3g,android,smartphone,home      
574c4969b017ae6481db9a7c77328bc3,"",Yandex.Navigator,2015-05-01 11:04:48,70,3g,ios,smartphone,home      
574c4969b017ae6481db9a7c77328bc3,"",VK Client,2015-6-01 12:02:27,248,3g,ios,smartphone,home     
574c4969b017ae6481db9a7c77328bc3,"",Viber,2015-07-01 12:06:35,7,3g,ios,smartphone,home      
574c4969b017ae6481db9a7c77328bc3,"",VK Client,2015-08-01 12:23:26,86,3g,ios,smartphone,home     
574c4969b017ae6481db9a7c77328bc3,"",Talking Angela,2015-08-02 12:24:52,0,3g,ios,smartphone,home     
574c4969b017ae6481db9a7c77328bc3,"",My Talking Angela,2015-08-03 12:24:52,167,3g,ios,smartphone,home        
574c4969b017ae6481db9a7c77328bc3,"",Talking Angela,2015-08-04 12:27:39,34,3g,ios,smartphone,home        
</code></pre>

<p>I need to count quantity of days in every month to every <code>ID</code>.</p>

<p>If I try <code>df.groupby('ID')['used_at'].count()</code> I get quantity of visiting, how can I take and count <code>days</code> at <code>month</code>? </p>

<p>I think you need <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html" rel="nofollow"><code>groupby</code></a> by <code>ID</code>, <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.month.html" rel="nofollow"><code>month</code></a> and <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.day.html" rel="nofollow"><code>day</code></a> and aggregate <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.size.html" rel="nofollow"><code>size</code></a>:</p>

<pre><code>df1 = df.used_at.groupby([df['ID'], df.used_at.dt.month,df.used_at.dt.day ]).size()

print (df1)
ID                                used_at  used_at
574c4969b017ae6481db9a7c77328bc3  5        1          1
                                  6        1          1
                                  7        1          1
                                  8        1          1
                                           2          1
                                           3          1
                                           4          1
e990fae0f48b7daf52619b5ccbec61bc  5        1          2
                                           2          1
                                  6        1          3
dtype: int64
</code></pre>

<p>Or by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.date.html" rel="nofollow"><code>date</code></a> - it is same as by <code>year</code>, <code>month</code> and <code>day</code>:</p>

<pre><code>df1 = df.used_at.groupby([df['ID'], df.used_at.dt.date]).size()

print (df1)
ID                                used_at   
574c4969b017ae6481db9a7c77328bc3  2015-05-01    1
                                  2015-06-01    1
                                  2015-07-01    1
                                  2015-08-01    1
                                  2015-08-02    1
                                  2015-08-03    1
                                  2015-08-04    1
e990fae0f48b7daf52619b5ccbec61bc  2015-05-01    2
                                  2015-05-02    1
                                  2015-06-01    3
dtype: int64
</code></pre>

<p>Differences between <code>count</code> and <code>size</code> in <a class='doc-link' href="http://stackoverflow.com/documentation/pandas/1822/grouping-data/6874/aggregating-by-size-and-count#t=201609271029599912108">SO documentation</a>.</p>

python datetime pandas aggregate days