文章目录
1. pandas
1.1 导入相关库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False # 步骤二(解决坐标轴负数的负号显示问题)
1.2 读取数据
df = pd.read_csv('E:\学习\DataAnalyst-gbk1.csv',encoding='gbk')
1.3 添加一列数据
df['avg'] = (df.top+df.bottom)/2
1.4 绘制折线图
df.avg.plot(figsize=(100,6))
曲线是根据companyId绘制的,但由于公司较多,曲线看不出什么信息。所以我们根据平均工资的不同绘制不同平均工资的公司有多少。
df.avg.value_counts().sort_index().plot()
df.avg.value_counts()
得到的是不同平均工资的公司的数量,索引是平均工资,这样直接绘制得到的图没有规律,所以我们要先将平均工资进行排序,然后再绘制。
可以看到大部分的工资都集中在9-30k之间。
1.5 柱形图
df.avg.value_counts().sort_index().plot(kind='bar')
#df.avg.value_counts().sort_index().plot.bar()
绘制结果比较密集
可使用透视表进行绘制,透视表就可从不同维度进行划分:
df.pivot_table(index='city',columns='education',values='avg',aggfunc='count').plot(kind='bar',stacked=True)
绘制不同城市里对学历的招聘要求的公司数量。可以看到背景的公司最多,其次是上海;招聘要求本科占绝大部分比率。
df.pivot_table(index='city',columns='education',values='avg',aggfunc='count').plot(kind='barh',stacked=True)
kind = ‘barh’,图像方向改变。
1.6 直方图
针对1.5的柱形图数量太多的问题,可使用直方图划分区间来绘制。
df.avg.value_counts().sort_index().plot.hist(bins=20)
df.groupby('education').apply(lambda x:x.avg).unstack().T # 反堆叠
df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.hist(alpha=0.5,stacked=True,bins=20)
alpha是透明度。
1.7 箱线图
df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.box()
df.boxplot(column='avg',by='education')
1.8 密度图
df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.kde()
df.avg.plot.kde()
1.9 面积图
df.groupby('education').apply(lambda x:x.avg).unstack().T.plot.area()
df.pivot_table(index='avg',columns='education',aggfunc='count',values='companyId').plot.area()
1.10 散点图
df.groupby('companyId').aggregate(['mean','count','max']).avg.plot.scatter(x='mean',y='count')
matrix = df.groupby('companyId').aggregate(['mean','count','max']).avg
pd.plotting.scatter_matrix(matrix)
1.11 饼图
df.city.value_counts().plot.pie(figsize=(6,<