-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
213 lines (175 loc) · 9.07 KB
/
app.py
File metadata and controls
213 lines (175 loc) · 9.07 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import pandas as pd
import seaborn as sns
import plotly.express as px
import streamlit as st
import matplotlib.pyplot as plt
# Load CSS for custom styling
st.markdown(
"""
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f0f4f7;
}
h1 {
color: #2C3E50;
font-size: 36px;
text-align: center;
}
h2 {
color: #2980B9;
}
.header {
text-align: center;
margin: 20px 0;
}
.container {
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
</style>
""", unsafe_allow_html=True
)
# Title of the app
st.title("Luxury Hotel Data Analysis")
# Sidebar for navigation
st.sidebar.title("Navigation")
page = st.sidebar.radio("Go to", ["Home", "Data Analysis"])
if page == "Home":
st.markdown("<div class='header'><h1>Explore the World’s Best 50 Hotels</h1></div>", unsafe_allow_html=True)
st.info("Upload a CSV file containing hotel data to begin analysis.")
uploaded_file = st.file_uploader("Choose a CSV file", type=["csv"])
if uploaded_file is not None:
df = pd.read_csv(uploaded_file, encoding='ISO-8859-1')
# Show the first few rows of the dataset
st.subheader("Dataset Overview")
st.write(df.head())
st.write(f"Dataset contains **{df.shape[0]}** rows and **{df.shape[1]}** columns.")
else:
st.subheader("Data Analysis")
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
if uploaded_file is not None:
df = pd.read_csv(uploaded_file, encoding='ISO-8859-1')
# Display column names for debugging
st.write("Column Names in the DataFrame:")
st.write(df.columns.tolist())
# Data summary and analysis
st.write("Summary Statistics:")
st.write(df.describe())
# Distribution of Starting Rates
st.subheader("Distribution of Starting Rates of Hotels")
fig = px.histogram(df, x='Starting Rate in ($)', nbins=15, title='Distribution of Starting Rates',
labels={'Starting Rate in ($)': 'Starting Rate in ($)'})
st.plotly_chart(fig)
# Average Starting Rates by Location
st.subheader("Average Starting Rates by Location")
location_rates = df.groupby('Location')['Starting Rate in ($)'].mean().reset_index()
fig = px.bar(location_rates, x='Location', y='Starting Rate in ($)', title='Average Starting Rates by Location')
st.plotly_chart(fig)
# Filter Options
st.subheader("Filter Data")
min_rate, max_rate = st.slider("Select the range of Starting Rates",
min_value=int(df['Starting Rate in ($)'].min()),
max_value=int(df['Starting Rate in ($)'].max()),
value=(int(df['Starting Rate in ($)'].min()), int(df['Starting Rate in ($)'].max())))
min_rooms, max_rooms = st.slider("Select the range of Total Rooms",
min_value=int(df['Total Rooms'].min()),
max_value=int(df['Total Rooms'].max()),
value=(int(df['Total Rooms'].min()), int(df['Total Rooms'].max())))
filtered_data = df[(df['Starting Rate in ($)'].between(min_rate, max_rate)) &
(df['Total Rooms'].between(min_rooms, max_rooms))]
st.write(f"Filtered dataset contains **{filtered_data.shape[0]}** rows.")
st.dataframe(filtered_data)
# Most Expensive and Least Expensive Hotels
st.subheader("Most and Least Expensive Hotels")
# Checking if expected columns exist
required_columns = ['Name', 'Location', 'Starting Rate in ($)', 'Total Rooms']
missing_columns = [col for col in required_columns if col not in df.columns]
if missing_columns:
st.error(f"The following columns are missing from the dataset: {', '.join(missing_columns)}")
else:
most_expensive = df.loc[df['Starting Rate in ($)'].idxmax()]
least_expensive = df.loc[df['Starting Rate in ($)'].idxmin()]
st.write("Most Expensive Hotel:")
st.write(most_expensive[['Name', 'Location', 'Starting Rate in ($)', 'Total Rooms']])
st.write("Least Expensive Hotel:")
st.write(least_expensive[['Name', 'Location', 'Starting Rate in ($)', 'Total Rooms']])
# Amenity Selection
st.subheader("Filter by Amenities")
# Handling NaN values in the amenities column
df['Hotel Ammenties'] = df['Hotel Ammenties'].fillna('')
amenities = df['Hotel Ammenties'].str.split(',').explode().str.strip().unique()
selected_amenities = st.multiselect("Select amenities to filter", options=amenities)
if selected_amenities:
# Create a boolean mask for filtering
amenities_mask = df['Hotel Ammenties'].str.contains('|'.join(selected_amenities), case=False)
filtered_amenities = df[amenities_mask]
st.write(f"Filtered by amenities dataset contains **{filtered_amenities.shape[0]}** rows.")
st.dataframe(filtered_amenities)
# Correlation Heatmap
st.subheader("Correlation Heatmap")
numeric_df = df.select_dtypes(include=['int64', 'float64'])
correlation_matrix = numeric_df.corr()
fig = plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f", linewidths=.5)
st.pyplot(fig)
# Distribution of Starting Rates
st.subheader("Distribution of Starting Rates of Hotels")
plt.figure(figsize=(10, 6))
sns.histplot(df['Starting Rate in ($)'], kde=True, color='lavender', bins=15)
plt.title('Distribution of Starting Rates of Hotels', fontsize=16)
plt.xlabel('Starting Rate in ($)', fontsize=12)
plt.ylabel('Frequency', fontsize=12)
st.pyplot(plt)
# Total Rooms vs. Starting Rates of Hotels
st.subheader("Total Rooms vs. Starting Rates of Hotels")
plt.figure(figsize=(10, 6))
sns.scatterplot(data=df, x='Total Rooms', y='Starting Rate in ($)', hue='Location', palette='viridis', s=100)
plt.title('Total Rooms vs. Starting Rates of Hotels', fontsize=16)
plt.xlabel('Total Rooms', fontsize=12)
plt.ylabel('Starting Rate in ($)', fontsize=12)
plt.legend(loc='upper right', title='Location')
plt.tight_layout()
st.pyplot(plt)
# Top 5 Dining Areas by Max Starting Rate
st.subheader("Top 5 Dining Areas by Max Starting Rate")
top_dining_areas = df.groupby('Dining Area')['Starting Rate in ($)'].max().sort_values(ascending=False).head(5)
plt.figure(figsize=(10, 6))
sns.barplot(x=top_dining_areas.values, y=top_dining_areas.index, palette='coolwarm')
plt.title('Top 5 Dining Areas by Max Starting Rate', fontsize=16)
plt.xlabel('Max Starting Rate in ($)', fontsize=12)
plt.ylabel('Dining Area', fontsize=12)
plt.tight_layout()
st.pyplot(plt)
# Starting Rates by Amenities (Swimming Pool and Spa)
st.subheader("Starting Rates by Amenities (Swimming Pool and Spa)")
df['Has Swimming Pool'] = df['Hotel Ammenties'].str.contains('Swimming pool', case=False, na=False)
df['Has Spa'] = df['Hotel Ammenties'].str.contains('spa/wellness centre', case=False, na=False)
amenity_melted = df.melt(id_vars=['Starting Rate in ($)'], value_vars=['Has Swimming Pool', 'Has Spa'],
var_name='Amenity', value_name='Available')
plt.figure(figsize=(12, 6))
sns.boxplot(x='Amenity', y='Starting Rate in ($)', hue='Available', data=amenity_melted, palette='Set2')
plt.title('Starting Rates by Amenities (Swimming Pool and Spa)', fontsize=16)
plt.xlabel('Amenity', fontsize=12)
plt.ylabel('Starting Rate in ($)', fontsize=12)
plt.legend(title='Availability')
plt.tight_layout()
st.pyplot(plt)
# Top 5 Hotel Amenities Distribution
st.subheader("Top 5 Hotel Amenities Distribution")
amenity_distribution = df['Hotel Ammenties'].str.split(',').explode().str.strip().value_counts()
top_amenities = amenity_distribution.head(5)
plt.figure(figsize=(8, 8))
plt.pie(top_amenities, labels=top_amenities.index, autopct='%1.1f%%', startangle=140, colors=sns.color_palette('pastel'))
plt.title('Top 5 Hotel Amenities Distribution', fontsize=16)
plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
st.pyplot(plt)
# Export Filtered Results
st.subheader("Export Filtered Results")
if st.button("Download Filtered Data as CSV"):
filtered_data.to_csv('filtered_data.csv', index=False)
st.success("Filtered data has been saved as 'filtered_data.csv'.")
else:
st.warning("Please upload a CSV file to start the analysis.")