I have a pandas data frame with investment data. Two columns - Year and Amount. I want to filter this dataframe(and ultimately a graph of this dataframe) by the years input by the user.
For example the data frame has 50 rows, but I want the user to be able to select a number of years less than 50 to have the dataframe then only show the number of years the user inpput so that my graph only shows the years the user input.
TL;DR - Can I filter the number of rows shown in a dataframe based on a value input by the user?
Hey @jareda,
Yes, you can filter a pandas dataframe based on some condition. Let us take your example i.e. you have two columns - Years and Amount. Now let’s say that a user inputs the years as: 2015, 2016, 2012, 2018.
What you can do is as follows:
# Make user's input as a list. Now it can be done in multiple ways. I am showing a simple example for now.
years_list = [2015, 2016, 2012, 2018]
filtered_df = df[df['Years'].isin(years_list)] # This line will give only those rows where 'year' falls in years_list.
filtered_df.head() # Now you can see that filtered dataframe.
In a similar way, you can apply any filter. Now you can take input from the user and structure it in a proper format and then apply the technique shown above. Similarly, you can plot using filtered_df.plot().
Thank you so much for the quick response. How would I update the code you posted to instead of choosing each they would select the number of years? Lets say the years column has years 1-50. And I am asking the user to select a number via a streamlit slider to say 25 years. I would then want the dataframe to only show years 1-25.
import streamlit as st
num_yrs = st.slider('Select number of years', min_value=1, max_value=50) # Getting the input.
df = df[df['Years'] <= num_yrs] # Filtering the dataframe.
st.dataframe(df) # Displaying the dataframe.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.