Matplotlib/Seaborn on calculated value (Pandas Dat

2019-07-31 02:32发布

I have the following table (Sorry for the format):

Date         Service Reference  Document
2018-05-14   A       Null       3542523
2018-05-15   B       01         6234242
2018-05-16   A       09         2342146 
2018-05-16   C       Null       2342342

I have a calculated value [Calculated] that is the

Reference.count/Document.count()

I want to create a graph similar to the next one:

enter image description here

Where in the x-axis I have the date, on the y axis the calculated column but shown with different lines representing the different Services.

So far I have this:

def calculate(df):
    return df.Reference.count() / df.Document.count()

df1 = df.groupby(['Date']).apply(calculate)

However if I try to add Services to the groupby I cannot plot it using

sns.lineplot()

Is there another way or an easier way to add the Services dimension to the plot?

Thanks

1条回答
倾城 Initia
2楼-- · 2019-07-31 03:11

Once you aggregate your data by date and service using:

df1 = df.groupby(['Date', 'Service']).apply(calculate)

Then, reset the index to convert to a dataframe (from a series)

df1 = df1.reset_index()

and then plot it:

sns.lineplot(x='Date', y=0, hue='Service', data=df1)
查看更多
登录 后发表回答