import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv('深圳罗湖二手房信息.csv',engine = 'python')
plt.figure(num=1)
plt.scatter(data['经度'],data['纬度'],s=data['房屋单价']/500,c=data['参考总价'],alpha=0.4,cmap='Reds')
plt.colorbar()
plt.grid()
def max_min(data):
return(data.max()-data.min())
key1 = data['参考首付']
key2 = data['参考总价']
key1_max_min = max_min(key1)
key2_max_min = max_min(key2)
通过直方图直接判断分组组数,简单查看数据分布,确定分布组数 → 一般8-16即可, 这里以10组为参考,总价的频率分布直方图
plt.figure(num=2)
key2.hist(bins=10)
求出参考总价的分组区间
zj_cut=pd.cut(key2,10,right=False)
zj_cut_count = zj_cut.value_counts(sort=False)
求出目标字段下频率分布的其他统计量 → 频数,频率,累计频率
x_zj = pd.DataFrame(zj_cut_count)
x_zj.rename(columns={zj_cut_count.name:'频数'},inplace=True)
x_zj['频率'] = x_zj['频数']/x_zj['频数'].sum()
x_zj['累计频率'] = x_zj['频率'].cumsum()
x_zj['频率%'] = x_zj['频率'].apply(lambda x:'%.2f%%'%(x*100))
x_zj['累计频率%'] = x_zj['累计频率'].apply(lambda x:'%.2f%%'%(x*100))
参考总价分布频率直方图
plt.figure(num=3)
x_zj['频率'].plot(kind='bar',
width=0.8,
figsize=(12,2),
rot=0,
alpha=0.4,
color='k')
#添加频数标签
for i,j,k in zip(range(len(x_zj)),x_zj['频率'],x_zj['频数']):
plt.text(i,j,k)
通过计数统计判断不同类别的频率,朝向数量统计、与上面一样的步骤
cx_count = data['朝向'].value_counts(sort=False)
x_cx = pd.DataFrame(cx_count)
x_cx.rename(columns={cx_count.name:'频数'},inplace=True)
x_cx['频率'] = x_cx['频数']/x_cx['频数'].sum()
x_cx['累计频率'] = x_cx['频率'].cumsum()
x_cx['频率%'] = x_cx['频率'].apply(lambda x:'%.2f%%'%(x*100))
x_cx['累计频率%'] = x_cx['累计频率'].apply(lambda x:'%.2f%%'%(x*100))
绘制频率直方图和饼图
plt.figure(num=4)
x_cx['频率'].plot(kind='bar',
width=0.8,
color='k',
alpha=0.4,
figsize=(12,2),
rot=0)
for i,j,k in zip(range(len(x_cx)),x_cx['频率'],x_cx['频数']):
plt.text(i,j,k)
plt.figure(num=5)
plt.pie(x_cx['频率'],labels=x_cx.index,autopct='%.2f%%')