数据分析
数据清洗
jupyter notebook 安装方法 pip install jupyter
jupyter notebook -h 查看帮助
jupyter notebook 启动jupyter终端
import pandas as pd
from matplotlib import pyplot as plt
new_df=pd.DataFrame({'A': [23, 22, 21],
'B': ['A', 'B', 'C],
'C': [10, 15, 8]}) #创建DataFrame对象
new_df['A'] #查看列数据
new_df.head(10) #查看前10行
new_df.tail(10) #查看后10行
new_df.info() #查看基本信息
new_df['数据列'].value_counts() #查看岗位数据分布
new_df['数据列'].value_counts() /new_df['数据列']. value_counts() .sum() #查看频率分布
a=pd.read_csv(path,encoding='utf-8')
a.index #获取索引列
a.values #获取
a['数据列'].min() #获取最小值
a['数据列'].max() #获取最大值
a['数据列'].mean() #获取平均值
a['数据列'].median() #获取中间值
a['数据列'].sum() #获取求和
a['数据列'].value_counts() #数据统计
#使用pandas绘图
a.plot(kind = 'pie', autopct = '%.2f%%', figsize = (7, 7),
title = '分布图', label = '') #饼状图绘制
a.plot(kind = 'bar', figsize = (13, 6), title = '分布图') #条形图
age.plot(kind = 'hist', bins = [17, 18, 19, 20, 21, 22, 23], figsize = (4,5), title = '直方图', grid = True) #bins分组描述 grid 网格显示
#数据清洗
s.isna() / isnull() #求空值
a.dropna() #去除空置
a.duplicated() #查找重复值
a.drop_duplicates() #去除重复值
a.describe() #查看异常值,求描述
a[a['类型A']>=x] #布尔索引
a.to_csv(path,encoding,index=False) #写数据 index是否写数据索引列
#处理日期
a1=pd.to_datetime(a['日期列'],format='%Y-%m-%d') #转化日期数据
year=a1.dt.year #切片 年
month=a1.dt.month #切片 月
day=a1.dt.day #切片 日
a['月份']=month #添加 月份 列
a.groupby('月份')['数据B列'].sum() #统计每个月份的数据B列情况
#图绘制
plt.reParams['font.family']=['Source Han Sans CN'] #图标文字汉化
plot_line_data.plot(kind='line',figsize=(7,7),title='折线图汇总') #绘制折线图
plot_pie_data.plot(kind='pie',autopct='%.2f%%,'figsize=(7,7),title='折线图汇总',label='') #绘制折线图 autopct精度设置
plot_bar_data.plot(kind='bar',figsize=(7,7),title='折线图汇总') #绘制柱状图
#拓展matplotlib画图
plt.figure(figsize=(9,9)) # 生成画布,设置画布大小
x=[1,2,3,4,5,6,7,8,9]
y=[9,8,7,6,5,4,3,2,1]
#折线图 ↓↓
plt.plot(x.y,linewidth=3,color ='r',marker = 'o',markerfacecolor = 'blue', markersize = 8) #标记点为蓝色,大小为8
plt.xticks(size=12) # 调整x轴刻度值的大小
plt.yticks(size=12) # 调整y轴刻度值的大小
plt.xlabel('x轴',fontdict={'size':15}) #添加X轴标签
plt.ylabel('y轴',fontdict={'size':15}) #添加Y轴标签
plt.title('折线图',fontsize=15) #设置标题 fontdict={'size':15}=fontsize=15
plt.savefig('保存路径')
#柱状图 ↓↓
plt.bar(x, height = y, color = 'blue', width = 1)
#病状图 ↓↓
plt.figure(figsize = (6, 6))
x = order_number.values
textprops = {'fontsize': 10, 'color': 'black'} #设置文本大小
explode = [0, 0, 0.05, 0, 0, 0] #设置explode参数 爆炸效果
plt.pie(x,autopct='%.2f%%',textprops=textprops,explode=explode) #画饼
plt.title('分布饼图', fontsize = 15) #设置标题
plt.legend(labels = order_number.index) #设置图例
plt.savefig('./工作/分布饼图.png') #保存
未完结时不时更新