Pandas Multicolumn Groupby Plotting
<p>Problem:<br>
I have a pandas dataframe of data that I would like to group-by year-months and rule_name.  Once grouped by I want to be able to get the counts of each of the rules during that period and the % of all the rules for that group. So far I am able to get each of the periods counts but not the percentage. </p>

<p>The goal is to have a plot similar to the ones at the bottom but on the right-y axis I would have percentage of the time period as well.</p>

<p>Goal Dataframes:<br>
For rule_name A:</p>

<pre><code>date       counts (rule_name)   %_rule_name 
Jan 16     1                   50
Feb 16     0                    0
Jun 16     2                   66
</code></pre>

<p>I would like to continue this for each rule_name (i.e. for B and C)</p>

<p>Code So Far:</p>

<pre><code>d  = {'date': ['1/1/2016', '2/1/2016', '3/5/2016', '2/5/2016', '1/15/2016', '3/3/2016', '3/4/2016'],
 'rule_name' : ['A' , 'B', 'C', 'C', 'B', 'A','A']}

df = pd.DataFrame(d)

Output:
</code></pre>

<p><a href="http://i.stack.imgur.com/hk3Sg.png" rel="nofollow"><img src="http://i.stack.imgur.com/hk3Sg.png" alt="enter image description here"></a></p>

<pre><code># format string date to datetime
df['date'] = pd.to_datetime(df['date'], format='%m/%d/%Y', errors='coerce')


rule_names = df['rule_name'].unique().tolist()
for i in rule_names:
    print ""
    print 'dataframe for', i ,':'
    df_temp = df[df['rule_name'] == i]
    df_temp = df_temp.groupby(df_temp['date'].map(lambda x: str(x.year) + '-' + str(x.strftime('%m')))).count()
    df_temp.plot(kind='line', title = 'Rule Name: ' + str(i))
    print df_temp

Output:
</code></pre>

<p><a href="http://i.stack.imgur.com/Fa1h6.png" rel="nofollow"><img src="http://i.stack.imgur.com/Fa1h6.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/NP2D7.png" rel="nofollow"><img src="http://i.stack.imgur.com/NP2D7.png" alt="enter image description here"></a></p>

<p>I feel like there is a better way to do this but am unable to figure it out.  I have been racking my brains on this problem for the last day'ish'.  Should I be filtering?  I tried a multi-index group-by but could not create a %_rule_name column.  Thanks for input in advance.</p>

<p>I was able to resolve this.  The following code provides the necessary plots and data processing.  I am putting it up in case this helps someone else.  It feels kind of janky but it gets the trick done.  Any suggestion to improve this would be appreciated.</p>

<p>Thanks SO.</p>

<pre><code>import seaborn as sns

df_all = df.groupby(df['date'].map(lambda x: str(x.year) + '-' + str(x.strftime('%m')))).count()
df_all = pd.DataFrame(df_all)
df_all['rule_name_all_count'] = df_all['rule_name']

rule_names = df['rule_name'].unique().tolist()
for i in rule_names:
    print ""
    print 'dataframe for', i ,':'
    df_temp = df[df['rule_name'] == i]
    df_temp = df_temp.groupby(df_temp['date'].map(lambda x: str(x.year) + '-' + str(x.strftime('%m')))).count()
    df_temp = pd.DataFrame(df_temp)
    df_merge = pd.merge(df_all, df_temp, right_index = True, left_index = True, how='left')
    drop_x(df_merge)
    rename_y(df_merge)
    df_merge.drop('date', axis=1, inplace=True)
    df_merge['rule_name_%'] = df_merge['rule_name'].astype(float) / df_merge['rule_name_all_count'].astype(float)
    df_merge = df_merge.fillna(0) 

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax2 = ax.twinx()

    df_merge['rule_name'].plot()
    df_merge['rule_name_%'].plot()
    plt.show()
    print df_temp
</code></pre>

<p><a href="http://i.stack.imgur.com/gU57a.png" rel="nofollow"><img src="http://i.stack.imgur.com/gU57a.png" alt="enter image description here"></a></p>

<p><a href="http://i.stack.imgur.com/K8dD1.png" rel="nofollow"><img src="http://i.stack.imgur.com/K8dD1.png" alt="enter image description here"></a></p>

python pandas plot group-by filtering