SeungHyun111 commited on
Commit
ebcd1da
ยท
verified ยท
1 Parent(s): 2ef80f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -59
app.py CHANGED
@@ -1,59 +1,54 @@
1
- import pandas as pd
2
- import matplotlib.pyplot as plt
3
- import seaborn as sns
4
- import matplotlib.font_manager as fm
5
- import streamlit as st
6
-
7
- # ํฐํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •
8
- font_path = r"C:\Users\user\Desktop\๋„์„œ๊ด€_๊ณต๋ชจ์ „\์ตœ์ข…\H2GTRM.TTF"
9
- font_prop = fm.FontProperties(fname=font_path, size=12)
10
- plt.rcParams['font.family'] = font_prop.get_name()
11
- plt.rcParams['axes.unicode_minus'] = False # ๋งˆ์ด๋„ˆ์Šค ๊ธฐํ˜ธ ๊นจ์ง ๋ฐฉ์ง€
12
-
13
- # ๋ฐ์ดํ„ฐ ๋กœ๋“œ
14
- file_path = r'C:\Users\user\Desktop\๋„์„œ๊ด€_๊ณต๋ชจ์ „\์ตœ์ข…\book_analysis_final.xlsx'
15
- df = pd.read_excel(file_path, sheet_name=2)
16
-
17
- # ์—ฐ๋ น๋Œ€ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ
18
- df['์—ฐ๋ น๋Œ€'] = df['์—ฐ๋ น'].astype(str)
19
-
20
- # ์žฅ๋ฅด๋ณ„ ๋Œ€์ถœ ๊ฑด์ˆ˜ ์ง‘๊ณ„
21
- genre_age_sex = df.groupby(['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„', '์ฃผ์ œ๋ถ„๋ฅ˜๋ช…'])['๋Œ€์ถœ๊ฑด์ˆ˜'].sum().unstack().fillna(0)
22
-
23
- # ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช… ์ถ”์ถœ
24
- top_genres = df.groupby(['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„', '์ฃผ์ œ๋ถ„๋ฅ˜๋ช…'])['๋Œ€์ถœ๊ฑด์ˆ˜'].sum().reset_index()
25
- top_genres = top_genres.sort_values(by=['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„', '๋Œ€์ถœ๊ฑด์ˆ˜'], ascending=[True, True, False])
26
-
27
- # ๊ฐ ์—ฐ๋ น๋Œ€์™€ ์„ฑ๋ณ„๋ณ„๋กœ ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช…์„ ์ถ”์ถœ
28
- top_3_genres = top_genres.groupby(['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„']).head(3).reset_index(drop=True)
29
-
30
- # Streamlit ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •
31
- st.title('์—ฐ๋ น/์„ฑ๋ณ„์— ๋”ฐ๋ฅธ ์ƒ์œ„ ์žฅ๋ฅด')
32
-
33
- # ์„ ํƒ ๋ฐ•์Šค ์ƒ์„ฑ
34
- selected_age_group = st.selectbox('์—ฐ๋ น๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”:', df['์—ฐ๋ น๋Œ€'].unique())
35
- selected_gender = st.selectbox('์„ฑ๋ณ„์„ ์„ ํƒํ•˜์„ธ์š”:', df['์„ฑ๋ณ„'].unique())
36
-
37
- # ์„ ํƒ๋œ ์นดํ…Œ๊ณ ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง
38
- filtered_data = top_3_genres[(top_3_genres['์—ฐ๋ น๋Œ€'] == selected_age_group) & (top_3_genres['์„ฑ๋ณ„'] == selected_gender)]
39
-
40
- # ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช… ์‹œ๊ฐํ™”
41
- if not filtered_data.empty:
42
- st.subheader(f'{selected_age_group} - {selected_gender}์˜ ์ƒ์œ„ 3 ์žฅ๋ฅด')
43
-
44
- plt.figure(figsize=(10, 6))
45
- sns.barplot(data=filtered_data, x='์ฃผ์ œ๋ถ„๋ฅ˜๋ช…', y='๋Œ€์ถœ๊ฑด์ˆ˜', palette='viridis')
46
- plt.title(f'{selected_age_group} - {selected_gender}์˜ ์ƒ์œ„ 3 ์žฅ๋ฅด')
47
- plt.xlabel('์ฃผ์ œ๋ถ„๋ฅ˜๋ช…')
48
- plt.ylabel('๋Œ€์ถœ๊ฑด์ˆ˜')
49
- plt.xticks(rotation=45)
50
- plt.tight_layout()
51
- st.pyplot(plt)
52
- else:
53
- st.write('์„ ํƒ๋œ ์—ฐ๋ น๋Œ€์™€ ์„ฑ๋ณ„์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.')
54
-
55
- # ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช… ์—‘์…€๋กœ ์ €์žฅ (์ถ”๊ฐ€ ์˜ต์…˜)
56
- if st.button('์ƒ์œ„ 3 ์žฅ๋ฅด ์—‘์…€๋กœ ์ €์žฅ'):
57
- excel_path = r'C:\Users\user\Desktop\์ƒ์œ„_3_์žฅ๋ฅด.xlsx'
58
- top_3_genres.to_excel(excel_path, index=False)
59
- st.write(f'์—‘์…€ ํŒŒ์ผ์ด ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: {excel_path}')
 
1
+ import pandas as pd
2
+ import matplotlib.pyplot as plt
3
+ import seaborn as sns
4
+ import matplotlib.font_manager as fm
5
+ import streamlit as st
6
+
7
+ # ํฐํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •
8
+ font_path = r"H2GTRM.TTF"
9
+ font_prop = fm.FontProperties(fname=font_path, size=12)
10
+ plt.rcParams['font.family'] = font_prop.get_name()
11
+ plt.rcParams['axes.unicode_minus'] = False # ๋งˆ์ด๋„ˆ์Šค ๊ธฐํ˜ธ ๊นจ์ง ๋ฐฉ์ง€
12
+
13
+ # ๋ฐ์ดํ„ฐ ๋กœ๋“œ
14
+ file_path = r'book_analysis_final.xlsx'
15
+ df = pd.read_excel(file_path, sheet_name=2)
16
+
17
+ # ์—ฐ๋ น๋Œ€ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ
18
+ df['์—ฐ๋ น๋Œ€'] = df['์—ฐ๋ น'].astype(str)
19
+
20
+ # ์žฅ๋ฅด๋ณ„ ๋Œ€์ถœ ๊ฑด์ˆ˜ ์ง‘๊ณ„
21
+ genre_age_sex = df.groupby(['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„', '์ฃผ์ œ๋ถ„๋ฅ˜๋ช…'])['๋Œ€์ถœ๊ฑด์ˆ˜'].sum().unstack().fillna(0)
22
+
23
+ # ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช… ์ถ”์ถœ
24
+ top_genres = df.groupby(['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„', '์ฃผ์ œ๋ถ„๋ฅ˜๋ช…'])['๋Œ€์ถœ๊ฑด์ˆ˜'].sum().reset_index()
25
+ top_genres = top_genres.sort_values(by=['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„', '๋Œ€์ถœ๊ฑด์ˆ˜'], ascending=[True, True, False])
26
+
27
+ # ๊ฐ ์—ฐ๋ น๋Œ€์™€ ์„ฑ๋ณ„๋ณ„๋กœ ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช…์„ ์ถ”์ถœ
28
+ top_3_genres = top_genres.groupby(['์—ฐ๋ น๋Œ€', '์„ฑ๋ณ„']).head(3).reset_index(drop=True)
29
+
30
+ # Streamlit ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •
31
+ st.title('์—ฐ๋ น/์„ฑ๋ณ„์— ๋”ฐ๋ฅธ ์ƒ์œ„ ์žฅ๋ฅด')
32
+
33
+ # ์„ ํƒ ๋ฐ•์Šค ์ƒ์„ฑ
34
+ selected_age_group = st.selectbox('์—ฐ๋ น๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”:', df['์—ฐ๋ น๋Œ€'].unique())
35
+ selected_gender = st.selectbox('์„ฑ๋ณ„์„ ์„ ํƒํ•˜์„ธ์š”:', df['์„ฑ๋ณ„'].unique())
36
+
37
+ # ์„ ํƒ๋œ ์นดํ…Œ๊ณ ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง
38
+ filtered_data = top_3_genres[(top_3_genres['์—ฐ๋ น๋Œ€'] == selected_age_group) & (top_3_genres['์„ฑ๋ณ„'] == selected_gender)]
39
+
40
+ # ์ƒ์œ„ 3๊ฐœ์˜ ์ฃผ์ œ๋ถ„๋ฅ˜๋ช… ์‹œ๊ฐํ™”
41
+ if not filtered_data.empty:
42
+ st.subheader(f'{selected_age_group} - {selected_gender}์˜ ์ƒ์œ„ 3 ์žฅ๋ฅด')
43
+
44
+ plt.figure(figsize=(10, 6))
45
+ sns.barplot(data=filtered_data, x='์ฃผ์ œ๋ถ„๋ฅ˜๋ช…', y='๋Œ€์ถœ๊ฑด์ˆ˜', palette='viridis')
46
+ plt.title(f'{selected_age_group} - {selected_gender}์˜ ์ƒ์œ„ 3 ์žฅ๋ฅด')
47
+ plt.xlabel('์ฃผ์ œ๋ถ„๋ฅ˜๋ช…')
48
+ plt.ylabel('๋Œ€์ถœ๊ฑด์ˆ˜')
49
+ plt.xticks(rotation=45)
50
+ plt.tight_layout()
51
+ st.pyplot(plt)
52
+ else:
53
+ st.write('์„ ํƒ๋œ ์—ฐ๋ น๋Œ€์™€ ์„ฑ๋ณ„์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.')
54
+