【问题标题】:Python Panel passing dataframe to param.ParameterizedPython Panel 将数据框传递给 param.Parameterized
【发布时间】:2020-02-19 11:34:23
【问题描述】:

在 param.Parameterized 类中传递数据帧的 Python 面板

我可以使用面板构建仪表板。我知道想将代码包含在一个包含数据操作的类中。

    df = ydata.load_web(rebase=True)
    class Plot(param.Parameterized):
        df = df
        col = list(df.columns)
        Index1 = param.ListSelector(default=col, objects=col)
        Index2 = param.ListSelector(default=col[1:2], objects=col)

        def dashboard(self, **kwargs):
            unds = list(set(self.Index1 + self.Index2))
            return self.df[unds].hvplot()

    b = Plot(name="Index Selector")
    pn.Row(b.param, b.dashboard)

我想打电话

   b =  Plot(name="Index Selector", df=ydata.load_web(rebase=True))

【问题讨论】:

    标签: python dashboard panel-pyviz


    【解决方案1】:

    使用参数化的DataFrame和两种方法

    • 根据数据框中的可用列设置 ListSelector 和
    • 使用 hv.Overlay 创建图(包含每个选定列的单个图),

    代码可能如下所示:

    # Test data frame with two columns
    df = pd.DataFrame(np.random.randint(90,100,size=(100, 1)), columns=['1'])
    df['2'] = np.random.randint(70,80,size=(100, 1))
    
    class Plot(param.Parameterized):
        df = param.DataFrame(precedence=-1) # precedence <1, will not be shown as widget
        df_columns = param.ListSelector(default=[], objects=[], label='DataFrame columns')
    
        def __init__(self, **params):
            super(Plot, self).__init__(**params)
    
            # set the column selector with the data frame provided at initialization
            self.set_df_columns_selector() 
    
        # method is triggered each time the data frame changes
        @param.depends('df', watch=True)
        def set_df_columns_selector(self):
            col = list(self.df.columns)
            print('Set the df index selector when the column list changes: {}'.format(col))
            self.param.df_columns.objects = list(col) # set choosable columns according current df
            self.df_columns = [self.param.df_columns.objects[0]] # set column 1 as default
    
        # method is triggered each time the choosen columns change
        @param.depends('df_columns', watch=True)
        def set_plots(self):
            print('Plot the columns choosen by the df column selector: {}'.format(self.df_columns))
            plotlist = [] # start with empty list
            for i in self.df_columns:
                # append plot for each choosen column
                plotlist.append(hv.Curve({'x': self.df.index, 'y':self.df[i]}))
            self.plot = hv.Overlay(plotlist)
    
        def dashboard(self):
            return self.plot
    
    b = Plot(name="Plot", df=df)
    
    layout = pn.Row(b.param, b.dashboard)
    layout.app()
    #or:
    #pn.Row(b.param, b.dashboard)
    
    

    这样,参数化变量负责更新绘图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 2013-01-26
      • 2015-01-13
      • 2022-01-23
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多