Reputation: 685
how to create a function to change the dtypes of columns in a dataframe in streamlit where the user select the type that he want to convert the selected column to it?
I create a function that take a dataframe as argument than display in a dropdown list the types that can the user choose from it.
the problem is that when the user select the new type and the system print df.info()
the selected column still having the old type.
import pandas as pd
import streamlit as st
def transform(df):
types = {'-':None
,'Boolean': '?'
,'Byte': 'b'
,'Integer':'i'
,'Floating point': 'f'
,'Date Time': 'M'
,'Time': 'm'
,'Unicode String':'U'
,'Object': 'O'}
new_types = {}
expander_types = st.beta_expander('Convert Data Types')
for i, col in enumerate(df.columns):
txt = 'Convert {} from {} to:'.format(col, df[col].dtypes)
expander_types.markdown(txt, unsafe_allow_html=True)
new_types[i] = expander_types.selectbox('Field to be converted:',[*types],index=0,key=i)
st.text(" \n") #break line
btn1 = st.button('Get CSV')
if btn1:
download_file(df, types, new_types, "csv")
print("transform",df.info())
where is the error in my function ??
Upvotes: 0
Views: 1370
Reputation: 948
Pandas can convert data in existing columns to different datatypes. For most string, integer, float, or boolean data, use pd.convert_dtypes and for datetime use pd.to_datetime. If you need a custom output that isn't converting datatypes (like time instead of datetime), you can use df.apply with a function that inputs the data in its original form and outputs the data in the form you desire.
Upvotes: 2