How to change the variables type from object to string or numerical?

Greetings community!

I tried to load a table using streamlit and I encountered this problem , the content of the loaded varibale is if type object and my table contains strings and numerical vlues, is there a way to convert these values ? knowing that I need this data vizualse using plotly.
Here is the script :
uploaded_files = st.file_uploader(“:memo:Choose your inputs:”, accept_multiple_files=True, type=[‘txt’])

if uploaded_files:
    merged_results = pd.DataFrame()

    for uploaded_file in uploaded_files:
        # Read txt file content
        file = uploaded_file.read().decode("utf-8")
        rows = file.splitlines()
        # Define table rows
        results_table = [row.split("\t") for row in rows]
        # Create a table
        df_results = pd.DataFrame(results_table[1:], columns=results_table[0])
        # Merge results from all selected txt files
        merged_results = pd.concat([merged_results, df_results], ignore_index=True)

This is more of a pandas problem instead of Streamlit. :slight_smile:
Here’s my take - Do some data wrangling before passing it to Streamlit.

You can convert the mixed data types in your DataFrame by leveraging pandas type inference functions. Or specifically, you can use the pd.to_numeric method to convert numerical values where applicable.

Here’s an updated version of your script to ensure proper data type handling :

import streamlit as st
import pandas as pd
import plotly.express as px

# File uploader
uploaded_files = st.file_uploader(":memo: Choose your inputs:", accept_multiple_files=True, type=['txt'])

if uploaded_files:
    merged_results = pd.DataFrame()

    for uploaded_file in uploaded_files:
        # Read txt file content
        file = uploaded_file.read().decode("utf-8")
        rows = file.splitlines()
        
        # Define table rows
        results_table = [row.split("\t") for row in rows]
        
        # Create a DataFrame
        df_results = pd.DataFrame(results_table[1:], columns=results_table[0])

        # Convert data types
        df_results = df_results.apply(lambda col: pd.to_numeric(col, errors='ignore'))

        # Merge results from all selected txt files
        merged_results = pd.concat([merged_results, df_results], ignore_index=True)

    # Display the DataFrame
    st.write("Merged Results:", merged_results)

    # Convert numerical columns for visualization
    numeric_cols = merged_results.select_dtypes(include=['number']).columns.tolist()

    if numeric_cols:
        fig = px.bar(merged_results, x=numeric_cols[0], y=numeric_cols[1])
        st.plotly_chart(fig)
    else:
        st.warning("No numerical columns found for visualization.")
2 Likes

Thank you, it worked :slight_smile:

1 Like

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