Spaces:
Build error
Build error
| import streamlit as st | |
| import pandas as pd | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| import plotly.express as px | |
| from wordcloud import WordCloud | |
| # Set page title | |
| st.set_page_config(page_title='Layoffs Data Visualization') | |
| # Load the CSV file | |
| # Caching the data for improved performance | |
| def load_data(file_path): | |
| data = pd.read_csv(file_path) | |
| return data | |
| # Main function | |
| def main(): | |
| # Page title and description | |
| st.title("Layoffs Data Visualization") | |
| st.write("Explore and visualize layoffs data") | |
| # Load the data | |
| data = load_data("Data/layoffs_data.csv") | |
| # Sidebar options | |
| st.sidebar.title("Options") | |
| selected_option = st.sidebar.selectbox("Select an option", ("Data Preview", "Unique Industries", | |
| "Layoffs Count by Company", | |
| "Layoffs Count by Country", | |
| "Bar plot of layoffs by industry", | |
| "Line plot of layoffs over time", | |
| "Word Cloud of Industries", | |
| "Histogram of Laid Off Counts", | |
| "Scatter Plot", | |
| "List of Companies and Laid Off Employees")) | |
| # Data exploration | |
| if selected_option == "Data Preview": | |
| st.subheader("Data Preview") | |
| num_rows = st.number_input("Number of rows to display", min_value=1, value=len(data)) | |
| st.dataframe(data.head(num_rows)) | |
| elif selected_option == "Unique Industries": | |
| st.subheader("Unique Industries") | |
| unique_industries = data['Industry'].unique() | |
| st.write(unique_industries) | |
| elif selected_option == "Layoffs Count by Company": | |
| st.subheader("Layoffs Count by Company") | |
| company_counts = data['Company'].value_counts() | |
| st.write(company_counts) | |
| elif selected_option == "Layoffs Count by Country": | |
| st.subheader("Layoffs Count by Country") | |
| country_counts = data['Country'].value_counts() | |
| st.write(country_counts) | |
| # Data visualization | |
| elif selected_option == "Bar plot of layoffs by industry": | |
| st.subheader("Bar plot of layoffs by industry") | |
| industry_counts = data['Industry'].value_counts() | |
| fig, ax = plt.subplots(figsize=(10, 6)) | |
| sns.barplot(x=industry_counts.index, y=industry_counts.values, ax=ax) | |
| ax.set_xticklabels(industry_counts.index, rotation=90) | |
| st.pyplot(fig) | |
| elif selected_option == "Line plot of layoffs over time": | |
| st.subheader("Line plot of layoffs over time") | |
| data['Date'] = pd.to_datetime(data['Date']) | |
| layoffs_over_time = data.groupby('Date').sum()['Laid_Off_Count'] | |
| fig, ax = plt.subplots(figsize=(10, 6)) | |
| sns.lineplot(x=layoffs_over_time.index, y=layoffs_over_time.values, ax=ax) | |
| ax.set_xlabel("Date") | |
| ax.set_ylabel("Layoffs Count") | |
| st.pyplot(fig) | |
| elif selected_option == "Word Cloud of Industries": | |
| st.subheader("Word Cloud of Industries") | |
| industry_text = ' '.join(data['Industry'].dropna().values.tolist()) | |
| wordcloud = WordCloud(width=800, height=400).generate(industry_text) | |
| plt.figure(figsize=(10, 6)) | |
| plt.imshow(wordcloud, interpolation='bilinear') | |
| plt.axis("off") | |
| st.pyplot(plt) | |
| elif selected_option == "Histogram of Laid Off Counts": | |
| st.subheader("Histogram of Laid Off Counts") | |
| fig, ax = plt.subplots(figsize=(10, 6)) | |
| sns.histplot(data['Laid_Off_Count'], kde=True, ax=ax) | |
| st.pyplot(fig) | |
| elif selected_option == "Scatter Plot": | |
| st.subheader("Scatter Plot") | |
| fig = px.scatter(data, x='Funds_Raised', y='Laid_Off_Count', color='Stage') | |
| st.plotly_chart(fig) | |
| elif selected_option == "List of Companies and Laid Off Employees": | |
| st.subheader("List of Companies and Laid Off Employees") | |
| company_employees = data.groupby('Company').sum()['Laid_Off_Count'].reset_index() | |
| st.dataframe(company_employees) | |
| if __name__ == '__main__': | |
| main() | |