File size: 1,950 Bytes
585d667
 
65c0104
585d667
 
f0f3d5c
585d667
 
fa860a7
585d667
2d746e8
 
585d667
7e203ab
a508480
7e203ab
585d667
2d746e8
 
 
 
 
 
7e203ab
 
e00507c
585d667
 
2d746e8
f98391b
fa860a7
f98391b
 
3eef162
b3733ec
3eef162
b3733ec
3eef162
b3733ec
f98391b
3eef162
 
 
 
f98391b
 
3eef162
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
import pandas as pd
import streamlit as st
import matplotlib.pyplot as plt

# Read the CSV file into a Pandas DataFrame
df = pd.read_csv('cph_airbnb_listings.csv')

# Create a Streamlit app
st.title('Sorted and Filtered Data by Neighbourhood and Price')

# Allow user to add filters for neighbourhoods
selected_neighbourhoods = st.sidebar.multiselect('Select Neighbourhood(s)', df['neighbourhood'].unique())

# Allow user to add filter for room type
selected_room_type = st.sidebar.selectbox('Select Room Type', ['All', 'Private room', 'Entire home/apt', 'Shared room', 'Hotel room'])

# Allow user to set a price range filter
price_range = st.sidebar.slider('Select Price Range', min_value=0, max_value=1000, step=10, value=(0, 1000))

# Filter the DataFrame based on selected filters
filtered_df = df.copy()
if selected_neighbourhoods:
    filtered_df = filtered_df[filtered_df['neighbourhood'].isin(selected_neighbourhoods)]
if selected_room_type != 'All':
    filtered_df = filtered_df[filtered_df['room_type'] == selected_room_type]

# Display the filtered DataFrame
st.write('Below is the sorted and filtered data:')
st.write(filtered_df)

# Calculate the average price for each selected neighbourhood and room type
avg_prices = filtered_df.groupby(['neighbourhood', 'room_type'])['price'].mean().reset_index()

# Pivot the DataFrame to have neighbourhoods as index and room types as columns
avg_prices_pivot = avg_prices.pivot(index='neighbourhood', columns='room_type', values='price')

# Plot a bar chart for average price of both room types in each neighbourhood
if not avg_prices_pivot.empty:
    st.write('Bar Chart of Average Price for Each Neighbourhood and Room Type:')
    fig, ax = plt.subplots()
    avg_prices_pivot.plot(kind='bar', ax=ax)
    ax.set_xlabel('Neighbourhood')
    ax.set_ylabel('Average Price')
    plt.xticks(rotation=45, ha='right')
    st.pyplot(fig)
else:
    st.write('No data available for selected filters.')