ziran / page_demographics.py
krishaamer's picture
Add some explanations
872dd1d
import streamlit as st
import pandas as pd
@st.cache_data
def show(df):
st.title("Respondent Demographics")
st.markdown(
f"<h2 style='text-align: center;'>Study Level</h2>", unsafe_allow_html=True)
show_student_counts(df)
st.markdown(
f"<h2 style='text-align: center;'>Age Distribution</h2>", unsafe_allow_html=True)
show_student_age_ranking(df)
st.markdown(
f"<h2 style='text-align: center;'>University</h2>", unsafe_allow_html=True)
st.text("Universities with the most respondents are those where flyers were distributed.")
generate_university_ranking_table(df)
st.markdown(
f"<h2 style='text-align: center;'>Field of Study</h2>", unsafe_allow_html=True)
st.text("These fields of study have not been normalized.")
show_field_of_study_ranking(df)
st.markdown(
f"<h2 style='text-align: center;'>Personality Type</h2>", unsafe_allow_html=True)
show_mbti_ranking(df)
def generate_university_ranking_table(df):
# Count the number of respondents for each university and sort them
university_counts = df["你/妳在哪所大學念書?"].value_counts(
).sort_values(ascending=False).reset_index()
university_counts.columns = ['University', 'Number of Respondents']
# Display the table in Streamlit
st.table(university_counts)
def show_student_counts(df):
# Counting the number of undergraduate and graduate students in the field "你/妳的學習階段:"
student_counts = df['你/妳的學習階段:'].value_counts()
# Converting the Series to a DataFrame for better table formatting
student_counts_df = student_counts.reset_index()
student_counts_df.columns = ['Study Level', 'Number of Students']
# Display the DataFrame as a table in Streamlit
st.table(student_counts_df)
def show_mbti_ranking(df):
# Count the occurrences of each MBTI type and sort them
mbti_counts = df["你/妳的MBTI?(選填)"].value_counts(
).sort_values(ascending=False).reset_index()
mbti_counts.columns = ['MBTI Type', 'Number of Students']
# Display the DataFrame as a table in Streamlit
st.table(mbti_counts)
def show_field_of_study_ranking(df):
# Count the occurrences of each field of study and sort them
field_of_study_counts = df["你/妳的學習科系:"].value_counts(
).sort_values(ascending=False).reset_index()
field_of_study_counts.columns = ['Field of Study', 'Number of Students']
# Display the DataFrame as a table in Streamlit
st.table(field_of_study_counts)
def show_student_age_ranking(df):
# Count the occurrences of each age and sort them
age_counts = df["你/妳幾歲?"].value_counts(
).sort_values(ascending=False).reset_index()
age_counts.columns = ['Age', 'Number of Students']
# Display the DataFrame as a table in Streamlit
st.table(age_counts)