遍历熊猫数据透视表以为Level = 0中的每个值生成Bokeh折线图

时间:2019-05-21 20:36:37

标签: python pandas bokeh

初学者/新手寻求帮助:

我正在尝试弄清楚如何在pd.pivot_table上进行迭代,因此我可以为级别= 0(数据透视表具有多个索引)的每个值创建散景折线图。

从整体上看数据时,一切都很好,但是现在我需要为每个“团队拥有”创建一个图表。

  1. 我已经更新了数据透视表,使其包含“拥有者分组”。当我查看df时,数据采用我期望的格式。当我将df传递给SLA_Chart函数时,它炸毁了, enter image description here enter image description here

    def PrepareDataSLAChart(df):
        # Change Datatypes 
        df['Date'] = pd.to_datetime(df['Date'])
    
        df = df.drop(['CreatedDate','Incident_ID'], axis='columns')
    
        df['Sum']df.groupby(['Owned_By_Team',pd.Grouper(key='Date',freq='M'),
        'Incident_Type'])['SLA_Resolution_Good'].transform(sum) 
    
    
        df['Count']df.groupby(['Owned_By_Team',pd.Grouper(key='Date',freq='M')
        ,'Incident_Type'])['Sum'].transform('count')
    
        df['Percent_of_SLA'] = round((df["Sum"] / df["Count"] * 100),2)
    
        # No longer need SLA Field
        df = df.drop(['SLA_Resolution_Good','Count','Sum'], axis='columns')
        df.index = df['Date']
        df['Date'] = pd.to_datetime(df['Date']) - MonthBegin(1)
    
        df = pd.pivot_table(df,index=['Owned_By_Team', 
        (pd.Grouper(key='Date',freq='M'))],columns='Incident_Type',
        values='Percent_of_SLA')
    
        return df
    
    def SLA_Chart(df):
        output_file("multiline_plot.html")
        source = ColumnDataSource(df)
        TOOLS = 'crosshair,save,pan,box_zoom,reset,wheel_zoom'
        p = figure(
           plot_width  = 1600,
           plot_height = 800,
           y_axis_type = "linear",
           x_axis_type = 'datetime', tools = TOOLS
           )
    
        # configure visual properties on a plot's title attribute
        p.title.text = 'Team Patriots'
        p.title.align = "center"
        p.title.text_color = "black"
        p.title.text_font_size = "25px"
    
        p.line('Date','Incident',source=source,
           legend='Incidents',
           line_color="blue",
           line_width = 3,
           line_cap = 'round')
    
        p.circle('Date','Incident',source=source,
            line_color='blue',
            line_width = 3)
    
        p.line("Date", "Service Request",source=source,
           legend="Service Requests",
           line_color="red",
           line_width = 3,
           line_cap = 'round')
    
        p.circle('Date','Service Request',source=source,
            line_color='red',
            line_width = 3)
    
        p.legend.location = "top_left"
    
        p.xaxis.axis_label = 'Date'
        p.yaxis.axis_label = '% Met'
    
        # open a browser
        return (show(p))
    
  2. 如何将我的SLA_Chart代码调整为a)接受df和 b)遍历df为每个团队拥有的人生成图表?

0 个答案:

没有答案