I am following along with the examples, and am currently on the “Save Filtered DF” one here: https://panel.holoviz.org/gallery/simple/save_filtered_df.html#simple-gallery-save-filtered-df
I would like to add an extra line that specifies the size of the filtered dataframe. I’ll just have a line of text saying “Filtered size: 2,000 records”. For this I’ll need to simply get len(df)
.
Now , I can’t figure out how to get the len(df)
back from the filtered mpg
function!
@pn.depends(years, mpg)
def filtered_mpg(yrs, mpg):
df = autompg
if years.value:
df = autompg[autompg.yr.isin(yrs)]
return df[(df.mpg >= mpg[0]) & (df.mpg <= mpg[1])]
Ideally I would like to get the number of records here. I have tried:
return df[(df.mpg >= mpg[0]) & (df.mpg <= mpg[1])] , len(df[(df.mpg >= mpg[0]) & (df.mpg <= mpg[1])])
I have also tried returning both of them in a list which I was hoping to index:
return [df[(df.mpg >= mpg[0]) & (df.mpg <= mpg[1])] , len(df[(df.mpg >= mpg[0]) & (df.mpg <= mpg[1])])]
I just need the size of the DF to put it here in the final line:
pn.Column(pn.Row(years, mpg), **text_stating_size_of_df**, fd, pn.panel(filtered_mpg, width=600), width=600).servable()
I came up with a very inefficient solution. I replicated the filtered_mpg
function, and made it only return a string with the size. But now there are two almost duplicate functions working on the same df
.
@pn.depends(years, mpg)
def filtered_mpg_size(yrs, mpg):
df = autompg
if years.value:
df = autompg[autompg.yr.isin(yrs)]
size_of_df = len(df[(df.mpg >= mpg[0]) & (df.mpg <= mpg[1])])
return f'Dataframe size: {size_of_df} records'
This works … but surely there is a more efficient or logical way. I am not a computer programmer so I think I’ve reached the limits of my “logic” thinking.
Any advice appreciated. Thanks