-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairbnb_data_streamlit.py
358 lines (310 loc) · 15.5 KB
/
airbnb_data_streamlit.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import pandas as pd
import mysql.connector
import streamlit as st
import plotly.express as px
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from PIL import Image
import webbrowser
#sql database
mydb= mysql.connector.connect(
host ='localhost',
user='root',
password=''
)
print(mydb)
mycursor=mydb.cursor(buffered=True)
mycursor.execute('''use airbnb''')
mydb.commit()
icon = Image.open(r"E:\streamlit\lgo.png")
st.set_page_config(page_title="Holiday Home & Appartment-Airbnb",
page_icon= icon,
layout="wide", initial_sidebar_state="auto")
with st.sidebar:
st.sidebar.markdown("# :rainbow[Select an option to filter:]")
selected = st.selectbox("**Menu**", ("Home","analysis","Top Charts","location_download","map_view"))
if selected=="Home":
cluum1,colum2 =st.columns([0.5,0.5])
with cluum1:
image =Image.open(r"E:\streamlit\air+_b\fdcb5df5-18bb-4563-aa81-6d1c7e2cbb19.webp")
st.image(image)
cluum13,colum20 =st.columns([0.5,0.5])
with cluum13:
image =Image.open(r"E:\streamlit\air+_b\90701f16-dbe7-4978-ab90-e0a186ec998e.webp")
st.image(image)
with colum20:
image =Image.open(r"E:\streamlit\air+_b\54c30ac7-c07c-4078-96be-aa16c1f50a07.webp")
st.image(image)
cluum101,colum122 =st.columns([0.5,0.5])
with cluum101:
image=Image.open(r"E:\streamlit\air+_b\54c30ac7-c07c-4078-96be-aa16c1f50a07.webp")
st.image(image)
with colum122:
image=Image.open(r"E:\streamlit\air+_b\3c1a5d33-42a7-485e-b885-e8cdc281b5d9.jpg")
st.image(image)
cluum10,colum12 =st.columns([0.5,0.5])
with cluum10:
image =Image.open(r"E:\streamlit\air+_b\90701f16-dbe7-4978-ab90-e0a186ec998e.webp")
st.image(image)
with colum12:
image =Image.open(r"E:\streamlit\air+_b\54c30ac7-c07c-4078-96be-aa16c1f50a07.webp")
st.image(image)
with colum2:
image =Image.open(r"E:\streamlit\air+_b\e7ee3078-43d1-4228-985f-74a62911a98c.webp")
st.image(image)
st.markdown('## :red[Project Title:]')
st.subheader(" Airbnb Analysis")
st.markdown('## :red[Skills takes away FRom This project:]')
st.subheader(" Python scarpint, Data Preprocessing, Visualization, EDA ,Streamlit ,Mongodb")
st.markdown('## :red[Domain:]')
st.subheader(" Travel industry, Property Management and Tourism")
colum3,colum4,colum5= st.columns([0.015,0.020,0.1])
with colum3:
if st.button("website"):
webbrowser.open_new_tab('https://www.airbnb.co.in/')
with colum4:
if st.button("show_map"):
webbrowser.open_new_tab('https://www.airbnb.co.in/sitemaps/v2')
if selected== "analysis":
selected = st.sidebar.selectbox("**Type**", ("price_analysis","review_analysis","review_for_city"))
# colum1,colum2 = st.columns([2,2])
# with colum1:
if selected =="price_analysis":
mycursor.execute('''select room_type,avg(price) as avg_price from airbnb_data2 group by room_type order by avg_price desc ''')
mydb.commit()
df=pd.DataFrame(mycursor,columns=mycursor.column_names)
# df
fig = px.bar(df, y='avg_price',
x='room_type',
title="group by room_type using avg price",
color='room_type',
color_discrete_sequence=px.colors.sequential.Agsunset)
# fig.show()
st.plotly_chart(fig,use_container_width=True)
# mycursor.execute('''select room_type,avg(price) as avg_price from airbnb_data2 group by room_type order by avg_price desc ''')
# mydb.commit()
# df=pd.DataFrame(mycursor,columns=mycursor.column_names)
# # df
# df['room_type']=df['room_type'].astype('str')
# df['room_type']=df['room_type'].str.replace('Shared room',"1")
# df['room_type']=df['room_type'].str.replace("Entire home/apt",'2')
# df['room_type']=df['room_type'].str.replace('Private room','3')
# df['room_type']=df['room_type'].astype('float')
# df
# x=df.index
# y=df['room_type']
# z=[0]
# dx=[1]
# dy=[1]
# dz=df['avg_price']
# fig=plt.figure(figsize=(8,4))
# ax=plt.axes(projection='3d')
# ax.bar3d(x,y,z,dx,dy,dz)
# st.pyplot(fig)
mycursor.execute('''select country, sum(price) as total_price from airbnb_data2 group by country order by total_price desc ''')
mydb.commit()
df=pd.DataFrame(mycursor,columns=mycursor.column_names)
# df
fig = px.bar(df, y='total_price',
x='country',
title="group by country using price",
color='total_price',
color_discrete_sequence=px.colors.sequential.Agsunset)
# fig.show()
st.plotly_chart(fig,use_container_width=True)
if selected=="review_analysis":
colum1,column2 = st.columns([1,1])
with colum1:
selecte_country = st.selectbox(
"select the country",
('United States','Canada','Spain','Australia','Brazil','Hong Kong','Portugal','China'))
colum1,column2 = st.columns([1,1])
with colum1:
mycursor.execute(f'''select country ,city,room_type, price ,review_rating,host_name from airbnb_data2
where country like "{selecte_country}"
group by city,review_rating,country order by review_rating desc , price desc limit 7''')
mydb.commit()
df=pd.DataFrame(mycursor,columns=mycursor.column_names)
# df
fig=px.sunburst(df,values='review_rating',
path=['country',"city",'room_type','review_rating','host_name'],
color='city',
template="plotly_dark",
title="top review_rating show price and city ")
# color_discrete_sequence=px.colors.sequential.Agsunset)
# fig.show()
st.plotly_chart(fig,use_container_width=True)
with column2:
fig=px.bar(df,x='city',y='price',color_discrete_sequence=px.colors.sequential.Agsunset)
st.plotly_chart(fig,use_container_width=True)
##################################################################################################
if selected=="review_for_city":
select_country = st.selectbox("select the country",('United States','Canada','Spain','Australia','Brazil','Hong Kong','Portugal','China'))
colum1,column2 = st.columns([1,1])
with colum1:
mycursor.execute(f'''select country,city,review_rating,number_of_reviews,price from airbnb_data2
where country like '{select_country}'
group by number_of_reviews order by number_of_reviews desc limit 7''')
mydb.commit()
df=pd.DataFrame(mycursor,columns=mycursor.column_names)
# df['review_rating']=df['review_rating'].astype('float')
# x=df.index
# y=df["number_of_reviews"]
# z=df['review_rating']
# dx=[1]
# dy=[1]
# dz=df['price']
# fig=plt.figure(figsize=(8,12))
# ax=plt.axes(projection='3d')
# ax.bar3d(x,y,z,dx,dy,dz)
# ax.set_xlabel('X-axis')
# ax.set_ylabel('Y-axis')
# ax.set_zlabel('Z-axis')
# # ax.set_title('3D Bar Chart')
# # plt.show()
# st.pyplot(fig,clear_figure=True)
fig=px.bar(df,x='city',y='number_of_reviews',color="price")
# color_discrete_sequence=px.colors.sequential.Agsunset)
# fig.show()
st.plotly_chart(fig,use_container_width=True)
with column2:
fig=px.sunburst(df,values='number_of_reviews',
path=['country','city',"number_of_reviews"],
color='number_of_reviews',
template="plotly_dark",
title="group by city top 7 number of review ",
color_discrete_sequence=px.colors.sequential.Agsunset)
# fig.show()
st.plotly_chart(fig,use_container_width=True)
if selected == "Top Charts":
selected_country = st.selectbox(
"select the country",
('United States','Canada','Spain','Australia','Brazil','Hong Kong','Portugal','China'))
mycursor.execute(f"""select city, country,sum(price) as total_price
from airbnb_data2 where country like '{selected_country}'
group by city order by total_price desc limit 10 """)
df = pd.DataFrame(mycursor, columns=mycursor.column_names)
# df
fig = px.bar(df,
# st.write('top 10 total_price city ')
title=selected_country,
x="city",
y="total_price",
color='total_price',
color_continuous_scale=px.colors.sequential.Agsunset)
# fig.show()
st.plotly_chart(fig,use_container_width=True)
if selected == "location_download":
selecte_country = st.selectbox(
"select the country",
('United States','Canada','Spain','Australia','Brazil','Hong Kong','Portugal','China'))
# selecte_city = st.selectbox(
# "select the city",
# ('Adalar','Agrela','Alexandria','Alfena','Annandale','Arcozelo','Arnavutköy','Arouca',
# 'Artarmon','Ãrvore','Ashfield','Astoria','AtaÅŸehir','AtaÅŸehir merkez','Austin','Avalon',
# 'Avalon Beach','Avcılar','Aveiro','Bağcılar','Bagunte','Bahçelievler','Bakırköy','Balgowlah','Balmain','Baltar',
# 'Barcelona','Barcelone','Bardwell Park','Barra da Tijuca','Barra da Tijuca - Ri','Barra de Guaratiba','BaÅŸakÅŸehir',
# 'Baulkham Hills','Beacon Hill','Beaconsfield','Bella Vista','Bellevue Hill','BeÅŸiktaÅŸ','BeÅŸiktaÅŸ/ bebek','Beverly Hills',
# 'Beykoz','Beylerbeyi','Beylikdüzü','Beylikdüzü Osb'))
select_room_type = st.selectbox(
"select the room_type",
('Entire home/apt','Private room','Shared room'))
select_property_type = st.selectbox(
"select the property_type",
('Aparthotel','Apartment','Barn','Bed and breakfast','Boat','Boutique hotel','Bungalow',
'Cabin','Camper/RV','Campsite','Casa particular (Cub','Castle','Chalet','Condominium',
'Cottage','Earth house','Farm stay','Guest suite','Guesthouse','Heritage hotel (Indi','Hostel','Hotel','House',
'Houseboat','Hut','Loft','Nature lodge','Other','Pension (South Korea','Resort',
'Serviced apartment','Tiny house','Townhouse','Train','Treehouse','Villa'))
mycursor.execute(f"""select country,city,property_type,room_type, price, host_name
from airbnb_data2
where country like '{selecte_country}' and room_type like '{select_room_type}' and property_type like '{select_property_type}'
group by country,city,room_type order by host_name,price desc""")
df = pd.DataFrame(mycursor, columns=mycursor.column_names)
# st.write(df)
# def color_survived(val):
# color = 'green' if val>200 else 'red'
# return f'background-color: {color}'
# st.dataframe(df.style.applymap(color_survived, subset=['price']))
st.expander("",expanded=False)
st.dataframe(df.style.background_gradient(cmap='Greens'),use_container_width=True)
# a=st.dataframe(df.style.applymap(color_survived, subset=['price']))
def convert_df(df):
return df.to_csv(index=False).encode('utf-8')
csv = convert_df(df)
st.download_button(
"Press to Download",
csv,
"file.csv",
"text/csv",
key='download-csv')
# st.button(df)
# fig = px.sunburst(df, values='price',
# path=['country',"city",'property_type','room_type','price','host_name'],
# color='property_type',
# # title=selected_state,
# color_discrete_sequence=px.colors.sequential.Agsunset)
# # fig.show()
# st.plotly_chart(fig,use_container_width=True)
if selected == "map_view":
selected_country = st.selectbox(
"select the country",
('United States','Canada','Spain','Australia','Brazil','Hong Kong','Portugal','China'))
mycursor.execute(f'''select country,city,longitude ,latitude,price, host_name
from airbnb_data2 where country like '{selected_country}' group by country,city order by price desc''')
mydb.commit()
df=pd.DataFrame(mycursor,columns=mycursor.column_names)
# df
# st.write(df.info())
# st.map(df,size='price',color="host_name")
import plotly.express as px
import pandas as pd
color_scale = [(0, 'orange'), (1,'red')]
fig = px.scatter_mapbox(df,
lat=df["latitude"],
lon=df["longitude"],
zoom=3,
color=df['price'],
hover_data=['host_name','city'],
color_continuous_scale=color_scale,
size=df['price'])
fig.update_layout(
mapbox_style="white-bg",
mapbox_layers=[
{
"below": 'traces',
"sourcetype": "raster",
# "sourceattribution": "United States Geological Survey",
"source": [
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
]
}
])
# mapbox_layers=[
# {
# "below": 'traces',
# "sourcetype": "raster",
# "sourceattribution": "United States Geological Survey",
# "source": [
# "https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
# ]
# },
# {
# "sourcetype": "raster",
# "sourceattribution": "Government of Canada",
# "source": ["https://geo.weather.gc.ca/geomet/?"
# "SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX={bbox-epsg-3857}&CRS=EPSG:3857"
# "&WIDTH=1000&HEIGHT=1000&LAYERS=RADAR_1KM_RDBR&TILED=true&FORMAT=image/png"],
# }
# ])
fig.update_layout(margin={"r":0,"t":50,"l":0,"b":10})
# fig.show()
st.plotly_chart(fig,use_container_width=True)
# hide = """
# <style>
# # footer {visibility: hidden;}
# # header {visibility: hidden;}
# </style>
# """
# st.markdown(hide,unsafe_allow_html = True)