streamlit3 / app.py
SeungHyun111's picture
Update app.py
ebcd1da verified
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.font_manager as fm
import streamlit as st
# ํฐํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •
font_path = r"H2GTRM.TTF"
font_prop = fm.FontProperties(fname=font_path, size=12)
plt.rcParams['font.family'] = font_prop.get_name()
plt.rcParams['axes.unicode_minus'] = False # ๋งˆ์ด๋„ˆ์Šค ๊ธฐํ˜ธ ๊นจ์ง ๋ฐฉ์ง€
# ๋ฐ์ดํ„ฐ ๋กœ๋“œ
file_path = r'book_analysis_final.xlsx'
df = pd.read_excel(file_path, sheet_name=2)
# ์—ฐ๋ น๋Œ€ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ
df['์—ฐ๋ น๋Œ€'] = df['์—ฐ๋ น'].astype(str)
# ์žฅ๋ฅด๋ณ„ ๋Œ€์ถœ ๊ฑด์ˆ˜ ์ง‘๊ณ„
genre_age_sex = df.groupby(['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„', '์ฃผ์ œ๋ถ„๋ฅ˜๋ช…'])['๋Œ€์ถœ๊ฑด์ˆ˜'].sum().unstack().fillna(0)
# ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช… ์ถ”์ถœ
top_genres = df.groupby(['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„', '์ฃผ์ œ๋ถ„๋ฅ˜๋ช…'])['๋Œ€์ถœ๊ฑด์ˆ˜'].sum().reset_index()
top_genres = top_genres.sort_values(by=['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„', '๋Œ€์ถœ๊ฑด์ˆ˜'], ascending=[True, True, False])
# ๊ฐ ์—ฐ๋ น๋Œ€์™€ ์„ฑ๋ณ„๋ณ„๋กœ ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช…์„ ์ถ”์ถœ
top_3_genres = top_genres.groupby(['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„']).head(3).reset_index(drop=True)
# Streamlit ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •
st.title('์—ฐ๋ น/์„ฑ๋ณ„์— ๋”ฐ๋ฅธ ์ƒ์œ„ ์žฅ๋ฅด')
# ์„ ํƒ ๋ฐ•์Šค ์ƒ์„ฑ
selected_age_group = st.selectbox('์—ฐ๋ น๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”:', df['์—ฐ๋ น๋Œ€'].unique())
selected_gender = st.selectbox('์„ฑ๋ณ„์„ ์„ ํƒํ•˜์„ธ์š”:', df['์„ฑ๋ณ„'].unique())
# ์„ ํƒ๋œ ์นดํ…Œ๊ณ ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง
filtered_data = top_3_genres[(top_3_genres['์—ฐ๋ น๋Œ€'] == selected_age_group) & (top_3_genres['์„ฑ๋ณ„'] == selected_gender)]
# ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช… ์‹œ๊ฐํ™”
if not filtered_data.empty:
st.subheader(f'{selected_age_group} - {selected_gender}์˜ ์ƒ์œ„ 3 ์žฅ๋ฅด')
plt.figure(figsize=(10, 6))
sns.barplot(data=filtered_data, x='์ฃผ์ œ๋ถ„๋ฅ˜๋ช…', y='๋Œ€์ถœ๊ฑด์ˆ˜', palette='viridis')
plt.title(f'{selected_age_group} - {selected_gender}์˜ ์ƒ์œ„ 3 ์žฅ๋ฅด')
plt.xlabel('์ฃผ์ œ๋ถ„๋ฅ˜๋ช…')
plt.ylabel('๋Œ€์ถœ๊ฑด์ˆ˜')
plt.xticks(rotation=45)
plt.tight_layout()
st.pyplot(plt)
else:
st.write('์„ ํƒ๋œ ์—ฐ๋ น๋Œ€์™€ ์„ฑ๋ณ„์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.')