Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas group by, filter & plot

I have a dataframe

Date         rule_name
Jan 1 2016   A
Feb 4 2016   B
Jun 6 2016   C
Feb 5 2016   B
Feb 9 2016   D
Jun 5 2016   A

And so on ...

I am hoping to get a dataframe for each rule similar to below: E.g. Dataframe for rule_name A:

date       counts (rule_name)   %_rule_name 
Jan 16     1                   100
Feb 16     0                    0
Jun 16     1                   50

E.g Dataframe for rule_name B:

date        counts (rule_name)   %_rule_name 
Jan 16      0                   0
Feb 16      2                   66.6
Jun 16      0                   0

Etc.

My current solution:

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

As you can see I am unable to get the % of rule name and am only plotting the count_rule_name. I feel like there is (a) a solution and (b) a better solution then iterating through the each rule name and plotting but am unable to figure it out unfortunately.

like image 874
SteelyDanish Avatar asked Nov 18 '25 15:11

SteelyDanish


1 Answers

Solution
Use df.Date.str.split().str[0] to get months

df.groupby([df.Date.str.split().str[0]]).rule_name.value_counts(True) \
  .unstack(fill_value=0).mul(100).round(1)

enter image description here

Plot

df.groupby([df.Date.str.split().str[0]]).rule_name.value_counts(True) \
  .unstack(fill_value=0).mul(100).round(1).plot.bar()

enter image description here

Validate Counts

df.groupby([df.Date.str.split().str[0], df.rule_name]).size().unstack(fill_value=0)

enter image description here

like image 61
piRSquared Avatar answered Nov 20 '25 05:11

piRSquared