简体   繁体   中英

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: Eg Dataframe for rule_name A:

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

Eg 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.

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)

在此输入图像描述

Plot

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

在此输入图像描述

Validate Counts

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM