Changing column types - object to int / float with a dropdown menu

Hi all. I’m Orkun, a Streamlit newby.

I looked on the web and some parts of the documentation but couldn’t be able to find a proper answer. So I decided to ask here, if this is irrelevant, please feel free to delete the post.

I am trying to build an A/B testing tool which creates academic tables for manuscripts as well. The tool will get a data file in .csv, .xlsx and .sav formats. However, as you all may know, the data types of the columns would not be proper for statistical analysis as they are in object type or something similar.

My first idea was to create a change_columns function, check if all the rows of the column are only numeric or strings and then change the types accordingly with pandas. However, It can be really slow and inefficient.

So I thought it would be a good idea if the users can change the data types with dropdown menus. And there comes my question here;

  • I want the user to choose columns and the app will show its data type
  • From a dropdown menu (or anything useful) will show desired data types - float16, object/category, int16 (16 is not a must of course)
  • And depending on the choices that the user made, my function will run df[col] = df[col].astype(selection)

Thank you in advance

All the best
Orkun

Hi @orkunaran,

Can you clarify what your question is? It sounds like you have a good idea of what you want to implement.

Caroline

Hi @Caroline
Sorry, yesterday was a busy day at work, I thought I asked my question. Sorry for the inconvenience.

My question is, what is the best solution to for the function? Is it the selectbox function or sth else.

all the best
Orkun

@orkunaran Since you have a fixed set of options to select from, a selectbox seems like a great option to me. But, in general, the best thing to do is just try it and see if it feels right. Streamlit makes iterating on an app super easy, so don’t feel like if you want to change your mind later it will be hard.

For example, it’s easy to change from

x = st.selectbox(options...)

to

x = st.select_slider(options...)

or even

x = st.text_input(...)

And no other part of the script will care what widget you used, it will happily just use whatever the value of x is.

Thank you for the detailed answer. I managed to solve my problem with ease. It seems it was a really basic question. Please excuse me if I wasted your time.

All the best
Orkun

1 Like

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