Penguni commited on
Commit
7421eb7
·
verified ·
1 Parent(s): c299a3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
4
 
 
5
  def assign_color(count):
6
  if count <= 10:
7
  return '1-10'
@@ -16,6 +17,7 @@ def assign_color(count):
16
  else:
17
  return '>1000'
18
 
 
19
  country_mapping = {
20
  'United States': 'USA',
21
  'United Kingdom': 'GBR',
@@ -81,7 +83,6 @@ country_mapping = {
81
  'Nigeria': 'NGA',
82
  'Ghana': 'GHA',
83
  'Ethiopia': 'ETH',
84
- 'South Africa': 'ZAF',
85
  'Botswana': 'BWA',
86
  'Namibia': 'NAM',
87
  'Zimbabwe': 'ZWE',
@@ -120,36 +121,35 @@ country_mapping = {
120
  'Cape Verde': 'CPV',
121
  'Seychelles': 'SYC',
122
  'Comoros': 'COM',
123
- 'Mauritius': 'MUS',
124
  'Maldives': 'MDV'
125
  }
126
 
127
-
128
  # Load your dataframes
129
- df_tv_series= pd.read_csv('series_after_cleaning.csv')
130
- df_movies= pd.read_csv('movie_after_cleaning.csv')
131
 
 
132
  df_movies['genre'] = df_movies['genre'].str.split(',')
133
  df_tv_series['genre'] = df_tv_series['genre'].str.split(',')
134
  df_movies['country'] = df_movies['country'].str.split(',')
135
  df_tv_series['country'] = df_tv_series['country'].str.split(',')
136
 
137
- # Function to generate treemap
138
  def create_treemap(df, title):
139
  fig = px.treemap(df, path=['parentalguide'], title=title)
140
  return fig
141
-
 
142
  def create_genre_bar_chart(df, title):
143
- # Explode the genre column to count each genre separately
144
  df_exploded = df.explode('genre')
145
  genre_counts = df_exploded['genre'].value_counts().reset_index()
146
  genre_counts.columns = ['genre', 'count']
147
- genre_counts = genre_counts.head(10) # Get top 10 genres
148
  fig = px.bar(genre_counts, x='count', y='genre', orientation='h', title=title)
149
  return fig
150
-
 
151
  def create_country_map(df, title):
152
- # Explode the country column to count each country separately
153
  df_exploded = df.explode('country')
154
  country_counts = df_exploded['country'].value_counts().reset_index()
155
  country_counts.columns = ['country', 'count']
@@ -157,7 +157,7 @@ def create_country_map(df, title):
157
  # Map country names to ISO codes
158
  country_counts['country'] = country_counts['country'].map(country_mapping)
159
 
160
- # Assign color ranges based on counts
161
  country_counts['color'] = country_counts['count'].apply(assign_color)
162
 
163
  fig = px.choropleth(country_counts,
@@ -168,18 +168,19 @@ def create_country_map(df, title):
168
  projection="natural earth",
169
  color_discrete_sequence=['#7FFF00', '#FFD700', '#FFA500', '#FF4500', '#DC143C', '#8B0000'],
170
  category_orders={"color": ['1-10', '10-50', '50-100', '100-500', '500-1000', '>1000']})
 
171
  return fig
172
 
173
  # Streamlit app
174
- st.title('Parental Guide Treemaps')
175
 
176
- # Split into two columns
177
  col1, col2 = st.columns(2)
178
 
179
- # Initialize variable for selection
180
  selection = 'Movies'
181
 
182
- # Add buttons in each column
183
  with col1:
184
  if st.button('Movies'):
185
  selection = 'Movies'
@@ -188,7 +189,7 @@ with col2:
188
  if st.button('TV Series'):
189
  selection = 'TV Series'
190
 
191
- # Display the corresponding treemap in the center
192
  if selection == 'Movies':
193
  st.plotly_chart(create_treemap(df_movies, 'Parental Guide - Movies'), use_container_width=True)
194
  st.plotly_chart(create_genre_bar_chart(df_movies, 'Top 10 Genres - Movies'), use_container_width=True)
@@ -196,5 +197,4 @@ if selection == 'Movies':
196
  elif selection == 'TV Series':
197
  st.plotly_chart(create_treemap(df_tv_series, 'Parental Guide - TV Series'), use_container_width=True)
198
  st.plotly_chart(create_genre_bar_chart(df_tv_series, 'Top 10 Genres - TV Series'), use_container_width=True)
199
- st.plotly_chart(create_country_map(df_tv_series, 'Global Distribution of TV Series'), use_container_width=True)
200
-
 
2
  import pandas as pd
3
  import plotly.express as px
4
 
5
+ # Function to assign color based on count
6
  def assign_color(count):
7
  if count <= 10:
8
  return '1-10'
 
17
  else:
18
  return '>1000'
19
 
20
+ # Country mapping dictionary
21
  country_mapping = {
22
  'United States': 'USA',
23
  'United Kingdom': 'GBR',
 
83
  'Nigeria': 'NGA',
84
  'Ghana': 'GHA',
85
  'Ethiopia': 'ETH',
 
86
  'Botswana': 'BWA',
87
  'Namibia': 'NAM',
88
  'Zimbabwe': 'ZWE',
 
121
  'Cape Verde': 'CPV',
122
  'Seychelles': 'SYC',
123
  'Comoros': 'COM',
 
124
  'Maldives': 'MDV'
125
  }
126
 
 
127
  # Load your dataframes
128
+ df_movies = pd.read_csv('movies.csv')
129
+ df_tv_series = pd.read_csv('tv_series.csv')
130
 
131
+ # Splitting genres and countries
132
  df_movies['genre'] = df_movies['genre'].str.split(',')
133
  df_tv_series['genre'] = df_tv_series['genre'].str.split(',')
134
  df_movies['country'] = df_movies['country'].str.split(',')
135
  df_tv_series['country'] = df_tv_series['country'].str.split(',')
136
 
137
+ # Function to create treemap
138
  def create_treemap(df, title):
139
  fig = px.treemap(df, path=['parentalguide'], title=title)
140
  return fig
141
+
142
+ # Function to create genre bar chart
143
  def create_genre_bar_chart(df, title):
 
144
  df_exploded = df.explode('genre')
145
  genre_counts = df_exploded['genre'].value_counts().reset_index()
146
  genre_counts.columns = ['genre', 'count']
147
+ genre_counts = genre_counts.head(10) # Top 10 genres
148
  fig = px.bar(genre_counts, x='count', y='genre', orientation='h', title=title)
149
  return fig
150
+
151
+ # Function to create choropleth map
152
  def create_country_map(df, title):
 
153
  df_exploded = df.explode('country')
154
  country_counts = df_exploded['country'].value_counts().reset_index()
155
  country_counts.columns = ['country', 'count']
 
157
  # Map country names to ISO codes
158
  country_counts['country'] = country_counts['country'].map(country_mapping)
159
 
160
+ # Assign color based on count
161
  country_counts['color'] = country_counts['count'].apply(assign_color)
162
 
163
  fig = px.choropleth(country_counts,
 
168
  projection="natural earth",
169
  color_discrete_sequence=['#7FFF00', '#FFD700', '#FFA500', '#FF4500', '#DC143C', '#8B0000'],
170
  category_orders={"color": ['1-10', '10-50', '50-100', '100-500', '500-1000', '>1000']})
171
+
172
  return fig
173
 
174
  # Streamlit app
175
+ st.title('Parental Guide Analysis')
176
 
177
+ # Split into two columns for buttons
178
  col1, col2 = st.columns(2)
179
 
180
+ # Default selection
181
  selection = 'Movies'
182
 
183
+ # Buttons for Movies and TV Series
184
  with col1:
185
  if st.button('Movies'):
186
  selection = 'Movies'
 
189
  if st.button('TV Series'):
190
  selection = 'TV Series'
191
 
192
+ # Display treemap, genre bar chart, and choropleth map based on selection
193
  if selection == 'Movies':
194
  st.plotly_chart(create_treemap(df_movies, 'Parental Guide - Movies'), use_container_width=True)
195
  st.plotly_chart(create_genre_bar_chart(df_movies, 'Top 10 Genres - Movies'), use_container_width=True)
 
197
  elif selection == 'TV Series':
198
  st.plotly_chart(create_treemap(df_tv_series, 'Parental Guide - TV Series'), use_container_width=True)
199
  st.plotly_chart(create_genre_bar_chart(df_tv_series, 'Top 10 Genres - TV Series'), use_container_width=True)
200
+ st.plotly_chart(create_country_map(df_tv_series, 'Global Distribution of TV Series'), use_container_width=True)