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(“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.
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.")
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.