However, there is a big risk of an X Y problem when answering a question like this.
What are you actually trying to achieve here? Try to explain to us the reason that you are asking this question in the first place - there may well be a different approach that would work better for you.
I have a text_input that take the user input than i run a query on the dataframe in order to filter the data and return matching records based on the user input
However the dataframe is consist of 4 columns
Id int
First object
Last object
Raking int
If the user enter a string (means that the query must filter the data based on object columns ) the return is correct
But if the user enter a number (means that the query must filter the data based on int column)the return is wrong because it consider the input as string.
Ok. So you can’t know in advance what the user will enter, and you want them to be able to enter either a number or a string?
As you have discovered, the input widget will just return a string.
In python, strings have methods, and you can call isnumeric() on the string to determine if it only contains numeric characters. There are other methods isdigit() or isdecimal() that you might with to consider.
If this test is true, you can cast it to an integer or a float depending on what type of number you’re after - just use int() or float() for this.
No, not really. You can’t test what the user input is before you’ve set up the widget to get the user input in the first place.
I would try something more like
user_input = st.text_input()
if user_input.isnumeric():
user_input = int(user_input)
Do stuff with a numeric input
else:
Do stuff with text-based input
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.