Only show first n rows of a dataframe

Hi,

is there a way to configure to only show the first n rows of a dataframe is streamlit?

Normal way to do this would be to just use df.head(n), however that does not quite work for my use case.

I have something like 10 rows and 5 columns. 3 of these columns represent scores and the other two are a name and a description.

I want to only show the top 5 scores, however if a user selects to sort the dataframe by a different score column (just via the standard streamlit ui), then the 5 top scores might change.

So a reduced example

Score 1 Score 2 Name
1 3 A
2 2 B
3 1 C

If the table is sorted by “Score 2” then A and B should be shown. But when the user clicks on the “Score 1” column to sort by it, it should now show C and B.

Is there any simple way to do that?

Cheers
Jan-Eric

Yeah…sort of.

Option 1:
You can set the height of your dataframe, effectively restricting the number of rows shown at a time, though a user could still scroll down.

Option 2:
You can implement your own sorting using column selections. Use .head(n) to display a dataframe you’ve sorted on the backend. Activate column selections and handle the sort on the backend when a user selects a column. Then you can show the first n rows of the newly sorted dataframe.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.