【问题标题】:Matplotlib/Seaborn on calculated value (Pandas Dataframe)Matplotlib/Seaborn 上的计算值(Pandas Dataframe)
【发布时间】:2019-01-19 15:29:24
【问题描述】:

我有下表(格式不对):

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],即

Reference.count/Document.count()

我想创建一个类似于下一个的图表:

x 轴上是日期,y 轴上是计算列,但用不同的线表示不同的服务。

到目前为止,我有这个:

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

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

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

绘制它
sns.lineplot()

是否有其他方法或更简单的方法可以将服务维度添加到绘图中?

谢谢

【问题讨论】:

    标签: python pandas seaborn


    【解决方案1】:

    一旦您按日期和服务汇总数据,使用:

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

    然后,重置索引以转换为数据帧(来自系列)

    df1 = df1.reset_index()
    

    然后绘制它:

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

    【讨论】:

      猜你喜欢
      • 2018-10-12
      • 2017-09-09
      • 2021-04-18
      • 1970-01-01
      • 2016-04-04
      • 2019-08-01
      • 2017-12-15
      • 2021-08-05
      相关资源
      最近更新 更多