Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python(Datapane):如何将动态变量传递到Datapane报告函数中_Python_Dataframe_Report_Altair - Fatal编程技术网

Python(Datapane):如何将动态变量传递到Datapane报告函数中

Python(Datapane):如何将动态变量传递到Datapane报告函数中,python,dataframe,report,altair,Python,Dataframe,Report,Altair,我正在开发一个图表模块,在这个模块中我可以传递dataframe,该模块将根据调用下面提到的几个函数生成的图创建报告 我使用Altair绘图,使用“Datapane”创建报告,其文档可在此处找到: 我的数据框看起来像这样 d = {'Date': ['2021-01-01', '2021-01-01','2021-01-01','2021-01-01','2021-01-02','2021-01-03'], 'country': ['IND','IND','IND','IND','I

我正在开发一个图表模块,在这个模块中我可以传递dataframe,该模块将根据调用下面提到的几个函数生成的图创建报告

我使用Altair绘图,使用“Datapane”创建报告,其文档可在此处找到:

我的数据框看起来像这样

d = {'Date': ['2021-01-01', '2021-01-01','2021-01-01','2021-01-01','2021-01-02','2021-01-03'], 
     'country': ['IND','IND','IND','IND','IND','IND' ],
     'channel': ['Organic','CRM','Facebook','referral','CRM','CRM' ],
     'sessions': [10000,8000,4000,2000,7000,6000 ],
     'conversion': [0.1,0.2,0.1,0.05,0.12,0.11 ],
      }
country_channel = pd.DataFrame(d)
绘图功能:

def plot_chart(source,Y_axis_1,Y_axis_2,chart_caption):
    base = alt.Chart(source).encode(
    alt.X('Date:T', axis=alt.Axis(title="Date"))
    )

    line_1 = base.mark_line(opacity=1, color='#5276A7').encode(
    alt.Y(Y_axis_1,
    axis=alt.Axis( titleColor='#5276A7'))
    )

   line_2 = base.mark_line(opacity=0.3,color='#57A44C', interpolate='monotone').encode(
    alt.Y(Y_axis_2,
          axis=alt.Axis( titleColor='#57A44C'))
   )

   chart_ae=alt.layer(line_1, line_2).resolve_scale(
    y = 'independent'
   ).interactive()

   charted_plot = dp.Plot(chart_ae , caption=chart_caption)
   return  charted_plot

def channel_plot_split(filter_1,filter_2,country,channel):
    channel_split_data = country_channel[(country_channel[filter_1]==country.upper())]
    channel_split_data =channel_split_data[(channel_split_data[filter_2].str.upper()==channel.upper())]
    channel_split_data=channel_split_data.sort_values(by='Date',ascending = True)
    channel_split_data=channel_split_data.reset_index(drop=True)
    channel_split_data.head()

    plot_channel_split = plot_chart(source=channel_split_data,Y_axis_1='sessions:Q',Y_axis_2='conversion:Q',chart_caption="Sessions-Conversion Plot for Country "+country.upper()+" and channel :"+ channel)
    channel_plot=dp.Group(dp.HTML("<div class='center'> <h3> Country : "+country.upper()+" & Channel : "+channel.upper()+"</h3></div>"),plot_channel_split,rows=2)
    return channel_plot

def grpplot(plot_1,plot_2):
    gp_plot = dp.Group(plot_1,plot_2,columns=2)
    return gp_plot
我现在可以通过调用datapane.report()函数生成报告,如下所示

r= dp.Report(row_1,row_2)

问题:当我知道有多少个频道时,这可以正常工作,但我的频道列表是动态的。我使用“for”循环生成行,但不确定如何在dp.Report()函数中将这些行作为kwargs传递。例如,如果我有10个通道,我需要动态传递10行

r= dp.Report(row_1,row_2)
  • 创建一个列表来存储报告的页面或元素,例如
    • 报告页面=[]
    • 报告页面追加(dp页面)
    • 报告附页(dp.表格)
    • 报告附页(dp.绘图)
  • 最后,只需使用指向列表的指针生成报告
    • dp.报告(*页)
    就你而言,我认为你可以做到以下几点

  • 创建一个列表
    • 行=[]
  • 将行添加到列表中
    • 行。追加(第1行)
    • 行。追加(第2行)
  • 然后使用创建报告
    • r=dp.报告(*行)
    我在datapane的github上找到了这个解决方案,然后在最后一行代码的笔记本中找到了


    我希望能有所帮助。

    下面是我如何解决这个问题的

    channel_graph_list=[]
    for i in range(0,len(unique_channels),1):
        channel_1_name = unique_channels[i]
        filtered_data = filter_the_data(source=channel_data,filter_1='channel',fv_1=channel_1_name)
        get_chart = plot_chart(filtered_data,Y_axis_1='sessions:Q',Y_axis_2='conversion:Q',chart_title='Session & Conv. Chart for '+channel_1_name)
        
        #This is where the trick starts - The below code creates a dynamic variable
        vars() ["channel_row_"+str(i)] = get_chart
         
        channel_graph_list.append("dp.Plot(channel_row_"+str(i)+",label='"+channel_1_name+"')")
        
    
    #convert the list to a string
    channel_graph_row = ','.join(channel_graph_list)
    
    # assign the code you want to run
    code="""channel_graph = dp.Select(blocks=["""+channel_graph_row+ """],type=dp.SelectType.TABS)"""
    #execute the code
    exec(code)
    

    希望上述解决方案能帮助其他希望将动态生成的参数传递到任何函数中的人。

    非常感谢您提供的解决方案。我尝试了列表技术,但无法直接传输列表。我用不同的逻辑解决了它。在这里回答这个问题是为了那些正在寻找这样一个解决方案的人。