Groupby Pandas dataframe and plot

I have a Pandas dataframe and wish to plot from a specific column by group. The goal is to have a separate graph for each org (like the image below).

Example plot

What is the most efficient way to accomplish this?

Source excerpt:

date                      time1           time2                time3     org                                                              
2017-11-16 02:14:20       36.46           20.42                16.04     abc
2017-11-21 08:39:15       40.12           10.18                29.94     abc
2017-11-06 08:21:08       33.00            0.00                33.00     bcd
2017-10-30 14:29:03       47.50           10.33                37.17     bcd
2017-11-09 07:51:48       31.92            1.92                30.00     efg
2017-11-07 08:53:05       33.00            1.04                31.96     efg

Attempted Code:

# load data
df = pd.read_csv('Dataset.csv')

# identify unique values in org col
org_group = sorted(df['org'].unique())

# set date as index for plot
df = df.set_index('date')
df.head()

for i in range(len(org_group)):
    # select specific org value from df
    df_group = df[df.org == org_group[i]]

    # display plot
    df_plot = df_group.resample('W').agg('median').plot(title="Median Times for "+org_group[i])
    plt.show()

    # iterate
    i = i+1

1 answer

  • answered 2018-01-11 19:45 Usernamenotfound

    If your dataframe is named df

    df = df[df.org == 'abc']
    

    will filter it for abc

    To get a list of unique items in org column use df.org.unique().tolist()

    Then you can iterate through the list and get a separate dataframe for each of the orgs