Updating a Param or Panel DataFrame object

I use many pandas.DataFrames in my existing code and am looking at moving my code to Param and Panel. I would like to visualize some resulting DataFrames after I process them from a file. I cannot find an example that shows how to update a Param.DataFrame without simply returning a new DataFrame from a function as part of a Panel interface.

The issue I have is I’m not sure how to attach other dependent code to selections or changes of the DataFrame data when doing it this way or in turn handle a class that starts off with empty data and then populates that data with a DataFrame once a file is processed as a result of selecting a file via a File input.

Any help in pointing me to a learning resources would be much appreciated.

Thanks,
Mike

Hi, I don’t know if I understand correctly want you want but you can use a param.DataFrame like it was a pd.DataFrame. First you create the param in a param.Parameterized class. Then you need to add a function that will return a display of this dataframe. This function will depends on the param.DataFrame. Thanks to that, each time you modify the DataFrame the function will reload a view of the updated DataFrame.

Here is an example :

   class Example(param.Parameterized):
       df=param.DataFrame(pd.DataFrame([],columns=['x','y']))
       add =param.Action(lambda x :x.param.trigger('add'))
       
       @param.depends('add',watch=True)
       def add_line(self):
           self.df=self.df.append(dict(zip(['x','y'],np.random.uniform(size=(2,)))),ignore_index=True)
           
       @param.depends('df')    
       def view_df(self):
           return pn.Row(self.df)

   ex=Example()

   pn.Row(ex.view_df,pn.Param(ex.param,parameters=['add']))

In this example, a new row is added to the dataframe each time you hit the button.

In your case you simply need to replace the add_line function by your preprocessing code linked with a pn.widget.FileInput

Is it possible to start with an empty DataFrame (i.e. prior to processing an input file) and then swapping out the entire param.DataFrame? The below seems to replace the param.DataFrame object with a regular pandas.DataFrame object. I won’t know the column headers ahead of time and I’d like to simply load in the data from a csv or some other file time and allow some crossfiltering type utility to the user.

The view code could hold the grouped and filtered data in a local df that updates as other param objects are changed on the class but the first part or getting the data loaded into the object from an unknown source seems to be the sticking point.

Sorry I’m obviously missing something very basic here.

    class Example(param.Parameterized)
        df=param.DataFrame(pd.DataFrame([],columns=['x','y']))
        load =param.Action(lambda x :x.param.trigger('load'))
   
       @param.depends('load', watch=True)
       def _load_df(self):
           # The below replaces the param.DataFrame with a regular pandas.DataFrame
           self.df = self._process_file_return_df()
       
       @param.depends('df')    
       def view_df(self):
           return pn.Row(self.df)

       @param.depends('df')    
       def view_filtered_df(self):
           local_df = self.df.groupby('SomeColumn').sum()
           return APlotOfTheFilteredDF

   ex=Example()
class Example(param.Parameterized):
    df=param.DataFrame()
    add =param.Action(lambda x :x.param.trigger('add'))
    col_filter = param.Selector(default='all', objects=['all', 'x', 'y'])

    @param.depends('add',watch=True)
    def add_line(self):
        if self.df is None:
            df = pd.DataFrame(np.random.uniform(size=(10,2)), columns=['x', 'y'])
        else:
            df = self.df.append(dict(zip(['x','y'],np.random.uniform(size=(2,)))),ignore_index=True)
        self.df=df

    @param.depends('df','col_filter')    
    def view_df(self):
        if self.col_filter == 'all':
            return pn.panel(self.df, width=500)
        else:
            return pn.panel(self.df[[self.col_filter]], width=500)

ex=Example()

pn.Row(ex.view_df,pn.Param(ex.param,parameters=['add', 'col_filter']))

1 Like