Extract our sp500_companies.csv to a Pandas DataFrame. Create a dash_table.DataTable. data is our df_companiesto a dictionary page_size is 25 sort_action is "native" Create a histogram. Our X-axis is "Sector". Create a title. ("Sector Distribution"?) Create a histogram. X-axis is "Sector". Y-axis is "Fulltimeemployees". histfunc is "avg". Create a title. Create a scatter plot. X-axis is "Marketcap". Y-axis is "Fulltimeemployees" color is "Sector". size is "Marketcap". Create a title. Either: Add a Dropdown that targets our histfunc ("count", "sum", "avg", "min", "max"). Add a @callback decorator. Our Input is the Dropdown mapped to value. Our Output is a Graph mapped to a figure generating a px.histogram.
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import plotly.express as px
# Set page title
st.title("S&P 500 Companies Analysis")
# Load data
@st.cache_data
def load_data():
try:
df = pd.read_csv("sp500_companies.csv")
# Convert column names to lowercase for consistency
df.columns = [col.lower() for col in df.columns]
return df
except Exception as e:
st.error(f"Error loading data: {e}")
return pd.DataFrame()
df_companies = load_data()
# Display DataTable
st.subheader("S&P 500 Companies")
st.dataframe(df_companies, height=400, use_container_width=True)
# Create histogram for Sector Distribution
st.subheader("Sector Distribution")
fig_sector = px.histogram(df_companies, x="sector")
st.plotly_chart(fig_sector, use_container_width=True)
# Add dropdown for histogram function
st.subheader("Average Full-time Employees by Sector")
histfunc_options = ["count", "sum", "avg", "min", "max"]
selected_histfunc = st.selectbox("Select Histogram Function", histfunc_options, index=2)
# Create histogram based on selection
fig_employees = px.histogram(
df_companies,
x="sector",
y="fulltimeemployees",
histfunc=selected_histfunc,
title=f"{selected_histfunc.capitalize()} of Full-time Employees by Sector"
)
st.plotly_chart(fig_employees, use_container_width=True)
# Create scatter plot
st.subheader("Market Cap vs Full-time Employees")
fig_scatter = px.scatter(
df_companies,
x="marketcap",
y="fulltimeemployees",
color="sector",
size="marketcap",
hover_name="name",
title="Market Cap vs Full-time Employees by Sector",
log_x=True # Using log scale for Market Cap for better visualization
)
st.plotly_chart(fig_scatter, use_container_width=True)
# Show data statistics
st.subheader("Data Statistics")
st.write(df_companies.describe())
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?