File size: 2,183 Bytes
ebcd1da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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('์„ ํƒ๋œ ์—ฐ๋ น๋Œ€์™€ ์„ฑ๋ณ„์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.')