基于全球短租平台数据集的可视化数据分析

数据集listing有上百个属性,只取其中所用到的列数据,做可视化

代码如下:

# 导入必要库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import cv2
import datetime
import chardet
import re
import os
import matplotlib.pyplot as plt  
plt.rcParams['font.sans-serif'] = [u'SimHei']
plt.rcParams['axes.unicode_minus'] = False
from collections import Counter
from PIL import Image
from wordcloud import WordCloud, ImageColorGenerator
from numba import jit
from pyecharts.charts import Line, Bar, Pie
from pyecharts import options as opts
from scipy import stats
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from statsmodels.tsa.seasonal import seasonal_decompose
from prophet import Prophet
import plotly.express as px


import pandas as pd

# 定义Excel文件的路径
excel_path = 'D:\ProgramData\jupyter notebook\dataset\\listing2000.xlsx'

# 指定需要读取的列
columns_to_use = ['price', 'room_type', 'property_type', 'accommodates', 
                  'number_of_reviews', 'review_scores_rating', 
                  'latitude', 'longitude', 'host_listings_count', 
                  'host_response_time', 'minimum_nights', 'maximum_nights', 
                  'availability_30', 'availability_60', 'availability_90']

# 使用pandas的read_excel函数读取指定列的数据
df = pd.read_excel(excel_path, usecols=columns_to_use)

# 显示数据框的前几行,以确认数据已正确加载
print(df.head())

# 检查空值
print("空值统计:")
print(df.isnull().sum())

# 填充空值
# 对于某些类别型数据,可以选择最频繁出现的值进行填充
df_clean = df.dropna(how='any')
#将处理之后的数据另存为新的文件
df.to_excel('cleaned_data.xlsx', index=False)


# 定义清洗后的Excel文件的路径
cleaned_excel_path = 'cleaned_data.xlsx'

# 读取Excel文件
df_cleaned = pd.read_excel(cleaned_excel_path)

print(df_cleaned.shape)

#可视化一:
#绘制直方图,显示价格的分布
plt.figure(figsize=(10, 6))
sns.histplot(df_cleaned['price'], bins=30, kde=True)
plt.title('Price Distribution')
plt.xlabel('Price')
plt.ylabel('Frequency')
plt.show()

#可视化二:
# 绘制箱型图
plt.figure(figsize=(10, 6))
sns.boxplot(x=['price'])
plt.title('Price Boxplot')
plt.ylabel('Price')
plt.show()


#可视化三:
# 2. 房间类型与价格
avg_price_by_room_type = df_cleaned.groupby('room_type')['price'].mean()
plt.figure(figsize=(10, 6))
avg_price_by_room_type.plot(kind='bar')
plt.title('房间类型与平均价格')
plt.xlabel('房间类型')
plt.ylabel('平均价格')
plt.show()


#可视化四:
#3 房源类型与可容纳人数
# 读取Excel文件
df = pd.read_excel('cleaned_data.xlsx')

# 确保'accommodates'列是数值类型
df['accommodates'] = pd.to_numeric(df['accommodates'], errors='coerce')

# 计算每个房源类型的平均可容纳人数,忽略NaN值
average_accommodates_by_type = df.groupby('property_type')['accommodates'].mean().reset_index()
# 确保结果集中的'property_type'是字符串类型
average_accommodates_by_type['property_type'] = average_accommodates_by_type['property_type'].astype(str)
# 为条形图排序(可选,为了更好的可视化效果)
average_accommodates_by_type = average_accommodates_by_type.sort_values('accommodates', ascending=False)
# 创建条形图
plt.figure(figsize=(10, 6))  # 可以根据需要调整图形大小
plt.barh(average_accommodates_by_type['property_type'], average_accommodates_by_type['accommodates'], color='skyblue')
# 设置图形标题和坐标轴标签
plt.title('Average Accommodates by Property Type')
plt.xlabel('Average Accommodates')
plt.ylabel('Property Type')
# 优化x轴的显示格式,如果是数值类型,可以设置旋转角度
plt.xticks(rotation=45)
# 显示图形
plt.tight_layout()  # 自动调整子图参数,使之填充整个图像区域
plt.show()

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值