-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
108 lines (87 loc) · 4.26 KB
/
Copy pathapp.py
File metadata and controls
108 lines (87 loc) · 4.26 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pandas as pd
import streamlit as st
import plotly.express as px
books_df = pd.read_csv('bestsellers_with_categories_2022_03_27.csv')
st.title("Bestselling Books Analysis")
st.write("This app alayzes the Amazom Top Selling books from 2009 - 2022")
# SIDEBAR
# Add new book
st.sidebar.header("Add New Book Data")
with st.sidebar.form("book_form"):
new_name = st.text_input("Book Name")
new_author = st.text_input("Author")
new_user_rating = st.slider("User Rating", 0.0, 5.0, 0.0, 0.1)
new_reviews = st.number_input("Reviews", min_value=0, step=1)
new_price = st.number_input("Price", min_value=0, step=1)
new_year = st.number_input("Year", min_value=2009, max_value=2022, step=1)
new_genre = st.selectbox("Genre", books_df['Genre'].unique())
submit_button = st.form_submit_button(label="Add Book")
if submit_button:
new_data = {
'Name': new_name,
'Author': new_author,
'User Rating': new_user_rating,
'Reviews': new_reviews,
'Price': new_price,
'Year': new_year,
'Genre': new_genre
}
books_df = pd.concat([pd.DataFrame(new_data, index=[0]), books_df], ignore_index=True)
books_df.to_csv('bestsellers_with_categories_2022_03_27.csv', index=False)
st.sidebar.success("New book added successfully!")
st.sidebar.header("Filter Options")
selected_author = st.sidebar.selectbox("Select Author", ["All"] + list(books_df['Author'].unique()))
selected_year = st.sidebar.selectbox("Select Year", ["All"] + list(books_df['Year'].unique()))
selected_genre = st.sidebar.selectbox("Select Genre", ["All"] + list(books_df['Genre'].unique()))
min_rating = st.sidebar.slider("Minimum User Rating", 0.0, 5.0, 0.0, 0.1)
max_price = st.sidebar.slider("Maximum price", 0, books_df['Price'].max(), books_df['Price'].max())
# Filter Options
filtered_books_df = books_df.copy()
if selected_author != "All":
filtered_books_df = filtered_books_df[filtered_books_df['Author'] == selected_author]
if selected_year != "All":
filtered_books_df = filtered_books_df[filtered_books_df['Year'] == selected_year]
if selected_genre != "All":
filtered_books_df = filtered_books_df[filtered_books_df['Genre'] == selected_genre]
filtered_books_df = filtered_books_df[(filtered_books_df['User Rating'] >= min_rating)
& (filtered_books_df['Price'] <= max_price)]
# Summary Statistics
st.subheader("Summary Statistics")
total_books = filtered_books_df.shape[0]
unique_titles = filtered_books_df['Name'].nunique()
average_rating = filtered_books_df['User Rating'].mean()
average_price = filtered_books_df['Price'].mean()
col1, col2, col3, col4 = st.columns(4, width=400)
col1.metric('Total books', total_books)
col2.metric('Unique titles', unique_titles)
col3.metric('Average rating', f"{average_rating:.2f}")
col4.metric('Average price', f"{average_price:.2f}")
# Dataset Preview
st.subheader("Dataframe Preview")
st.write(filtered_books_df.head())
# Book Title and Author Distribution
col_top_10_books, col_top_10_authors = st.columns(2)
with col_top_10_books:
st.subheader("Top 10 Book Titles")
top_titles = filtered_books_df['Name'].value_counts().head(10)
st.bar_chart(top_titles)
with col_top_10_authors:
st.subheader("Top 10 Authors")
top_authors = filtered_books_df['Author'].value_counts().head(10)
st.bar_chart(top_authors)
# Genre Distribution Pie Chart
st.subheader("Genre Distribution")
fig = px.pie(filtered_books_df, names='Genre', title='Most Liked Genre 2009 - 2022',
color_discrete_sequence=px.colors.sequential.Plasma)
st.plotly_chart(fig)
# Number of Fiction vs Non-Fiction Books Over the Years
st.subheader("Number of Fiction vs Non-Fiction Books Over the Years")
size = filtered_books_df.groupby(['Year', 'Genre']).size().reset_index(name='Counts')
fig = px.bar(size, x='Year', y='Counts', color='Genre', title='Number of Fiction vs Non-Fiction Books Over the Years'
, color_discrete_sequence= px.colors.sequential.Plasma, barmode='group')
st.plotly_chart(fig)
# Filter Data By Genre
st.subheader('Filter Data By Genre')
genre_filter = st.selectbox("Select Genre", filtered_books_df['Genre'].unique())
filtered_df = filtered_books_df[filtered_books_df['Genre'] == genre_filter]
st.write(filtered_df)