Matplotlib/Seaborn的计算值(Pandas数据框) [英] Matplotlib/Seaborn on calculated value (Pandas Dataframe)

查看:113
本文介绍了Matplotlib/Seaborn的计算值(Pandas数据框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表(格式很抱歉):

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

我有一个计算值[Calculated]就是

I have a calculated value [Calculated] that is the

Reference.count/Document.count()

我想创建一个与下一个相似的图:

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

在x轴上我有日期,在y轴上我有计算所得的列,但显示有代表不同服务的不同行.

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)

但是,如果我尝试将服务添加到groupby中,则无法使用

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?

谢谢

推荐答案

一旦您使用以下日期和服务来汇总数据:

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()

然后将其绘制:

sns.lineplot(x='Date', y=0, hue='Service', data=df1)

这篇关于Matplotlib/Seaborn的计算值(Pandas数据框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆