首页 > 其他分享 >数据分析3

数据分析3

时间:2023-03-15 14:35:50浏览次数:24  
标签:数据分析 sort plt selected child data id

# 代码8-1 查看数据特征

import numpy as np
import pandas as pd

inputfile = r"C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\GoodsOrder.csv" # 输入的数据文件
data = pd.read_csv(inputfile,encoding = 'gbk') # 读取数据
data .info() # 查看数据属性

data = data['id']
description = [data.count(),data.min(), data.max()] # 依次计算总数、最小值、最大值
description = pd.DataFrame(description, index = ['Count','Min', 'Max']).T # 将结果存入数据框
print('描述性统计结果:\n',np.round(description)) # 输出结果

 

 

 

# 代码8-2 分析热销商品

# 销量排行前10商品的销量及其占比
import pandas as pd
inputfile = r"C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\GoodsOrder.csv" # 输入的数据文件
data = pd.read_csv(inputfile,encoding = 'gbk') # 读取数据
group = data.groupby(['Goods']).count().reset_index() # 对商品进行分类汇总
sorted=group.sort_values('id',ascending=False)
print('销量排行前10商品的销量:\n', sorted[:10]) # 排序并查看前10位热销商品

# 画条形图展示出销量排行前10商品的销量
import matplotlib.pyplot as plt
x=sorted[:10]['Goods']
y=sorted[:10]['id']
plt.figure(figsize = (8, 4)) # 设置画布大小
plt.barh(x,y)
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.xlabel('销量') # 设置x轴标题
plt.ylabel('商品类别') # 设置y轴标题
plt.title('商品的销量TOP10-3029') # 设置标题
plt.savefig(r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\top10.png') # 把图片以.png格式保存
plt.show() # 展示图片

# 销量排行前10商品的销量占比
data_nums = data.shape[0]
for idnex, row in sorted[:10].iterrows():
print(row['Goods'],row['id'],row['id']/data_nums)

 

 




# 代码8-3 各类别商品的销量及其占比

import pandas as pd
inputfile1 = r"C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\GoodsOrder.csv"
inputfile2 = r"C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\GoodsTypes.csv"
data = pd.read_csv(inputfile1,encoding = 'gbk')
types = pd.read_csv(inputfile2,encoding = 'gbk') # 读入数据

group = data.groupby(['Goods']).count().reset_index()
sort = group.sort_values('id',ascending = False).reset_index()
data_nums = data.shape[0] # 总量
del sort['index']

sort_links = pd.merge(sort,types) # 合并两个datafreame 根据type
# 根据类别求和,每个商品类别的总量,并排序
sort_link = sort_links.groupby(['Types']).sum().reset_index()
sort_link = sort_link.sort_values('id',ascending = False).reset_index()
del sort_link['index'] # 删除“index”列

# 求百分比,然后更换列名,最后输出到文件
sort_link['count'] = sort_link.apply(lambda line: line['id']/data_nums,axis=1)
sort_link.rename(columns = {'count':'percent'},inplace = True)
print('各类别商品的销量及其占比:\n',sort_link)
outfile1 = r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\percent.csv'
sort_link.to_csv(outfile1,index = False,header = True,encoding='gbk') # 保存结果

# 画饼图展示每类商品销量占比
import matplotlib.pyplot as plt
data = sort_link['percent']
labels = sort_link['Types']
plt.figure(figsize=(8, 6)) # 设置画布大小
plt.pie(data,labels=labels,autopct='%1.2f%%')
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.title('每类商品销量占比-3029') # 设置标题
plt.savefig(r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\persent.png') # 把图片以.png格式保存
plt.show()

 

 

 

# 代码8-4 非酒精饮料内部商品的销量及其占比

# 先筛选“非酒精饮料”类型的商品,然后求百分比,然后输出结果到文件。
selected = sort_links.loc[sort_links['Types'] == '非酒精饮料'] # 挑选商品类别为“非酒精饮料”并排序
child_nums = selected['id'].sum() # 对所有的“非酒精饮料”求和
selected['child_percent'] = selected.apply(lambda line: line['id']/child_nums,axis = 1) # 求百分比
selected.rename(columns = {'id':'count'},inplace = True)
print('非酒精饮料内部商品的销量及其占比:\n',selected)
outfile2 =r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\child_percent.csv'
sort_link.to_csv(outfile2,index = False,header = True,encoding='gbk') # 输出结果

# 画饼图展示非酒精饮品内部各商品的销量占比
import matplotlib.pyplot as plt
data = selected['child_percent']
labels = selected['Goods']
plt.figure(figsize = (8,6)) # 设置画布大小
explode = (0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.3,0.1,0.3) # 设置每一块分割出的间隙大小
plt.pie(data,explode = explode,labels = labels,autopct = '%1.2f%%',
pctdistance = 1.1,labeldistance = 1.2)
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.title("非酒精饮料内部各商品的销量占比-3029") # 设置标题
plt.axis('equal')
plt.savefig(r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\child_persent.png') # 保存图形
plt.show() # 展示图形

 

 

 

selected = sort_links.loc[sort_links['Types'] == '西点']
child_nums = selected['id'].sum()
selected['child_percent'] = selected.apply(lambda line: line['id']/child_nums,axis = 1) # 求百分比
selected.rename(columns = {'id':'count'},inplace = True)
print('西点内部商品的销量及其占比:\n',selected)
outfile2 =r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\child_percent.csv'
sort_link.to_csv(outfile2,index = False,header = True,encoding='gbk') # 输出结果

# 画饼图展示西点内部各商品的销量占比
import matplotlib.pyplot as plt
data = selected['child_percent']
labels = selected['Goods']
plt.figure(figsize = (8,6)) # 设置画布大小
explode = (0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.05,0.05,0.05,0.06,0.06) # 设置每一块分割出的间隙大小
plt.pie(data,explode = explode,labels = labels,autopct = '%1.2f%%',
pctdistance = 1.1,labeldistance = 1.2)
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.title("西点内部各商品的销量占比-3029") # 设置标题
plt.axis('equal')
plt.savefig(r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\child_persent1.png') # 保存图形
plt.show() # 展示图形

 

 

selected = sort_links.loc[sort_links['Types'] == '百货']
child_nums = selected['id'].sum()
selected['child_percent'] = selected.apply(lambda line: line['id']/child_nums,axis = 1) # 求百分比
selected.rename(columns = {'id':'count'},inplace = True)
print('百货内部商品的销量及其占比:\n',selected)
outfile2 =r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\child_percent.csv'
sort_link.to_csv(outfile2,index = False,header = True,encoding='gbk') # 输出结果

# 画饼图展示百货内部各商品的销量占比
import matplotlib.pyplot as plt
data = selected['child_percent']
labels = selected['Goods']
plt.figure(figsize = (8,6)) # 设置画布大小
plt.pie(data,labels = labels,autopct = '%1.2f%%',
pctdistance = 1.1,labeldistance = 1.2)
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.title("百货内部各商品的销量占比-3029") # 设置标题
plt.axis('equal')
plt.savefig(r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\child_persent2.png') # 保存图形
plt.show() # 展示图形

 

import pandas as pd
inputfile=r'C:\Users\admin\Documents\WeChat Files\wxid_b0fz4hqogenr22\FileStorage\File\2023-03\GoodsOrder.csv'
data = pd.read_csv(inputfile,encoding = 'gbk')

# 根据id对“Goods”列合并,并使用“,”将各商品隔开
data['Goods'] = data['Goods'].apply(lambda x:','+x)
data = data.groupby('id').sum().reset_index()

# 对合并的商品列转换数据格式
data['Goods'] = data['Goods'].apply(lambda x :[x[1:]])
data_list = list(data['Goods'])

# 分割商品名为每个元素
data_translation = []
for i in data_list:
p = i[0].split(',')
data_translation.append(p)
print('数据转换结果的前5个元素:\n', data_translation[0:5])

 

标签:数据分析,sort,plt,selected,child,data,id
From: https://www.cnblogs.com/x3029/p/17218389.html

相关文章

  • Python 数据分析
    Python数据分析目录Python数据分析1Python中的数据分析2NumPy2.1ndarray创建多维数组2.2ndarray的属性2.3ndarray的基本操作2.3.1索引2.3.2切片2.3.3变形2.3.......
  • 数据分析学习-常用分析方法-总结二
    数据分析—常用分析方法一、5W2H分析方法5w:what(是什么)、when(何时)、where(何地)、why(为什么)、who(是谁)2h:how(怎么做)、howmuch(多少钱)案例一:如何是一款产品what:这是什么产......
  • python数据分析与挖掘实战第八章
    #8-1importnumpyasnpimportpandasaspdinputfile='data4/GoodsOrder.csv'data=pd.read_csv(inputfile,encoding='gbk')data.info()data=data['id']de......
  • 界面控件DevExtreme的Pivot Grid组件——轻松实现多维数据分析
    DevExtreme拥有高性能的HTML5/JavaScript小部件集合,使您可以利用现代Web开发堆栈(包括React,Angular,ASP.NETCore,jQuery,Knockout等)构建交互式的Web应用程序,该套件附带功能......
  • python 数据分析
    importmatplotlib.pyplotaspltimportpandasaspddatafile='air_data.csv'resultfile='explore.csv'data=pd.read_csv(datafile,encoding='utf-8')explore=dat......
  • 飞机客户数据分析预测
    代码一:读取数据importpandasaspddatafile='E:\\code\\PythonCode\\datas\\air_data.csv'resultfile='E:\\code\\PythonCode\\datas\\explore.csv'data=pd.read_......
  • python数据分析
    importmatplotlib.pyplotaspltimportpandasaspddatafile='air_data.csv'resultfile='explore.csv'data=pd.read_csv(datafile,encoding='utf-8')explore=da......
  • Python数据分析之航空公司客户价值分析
    #代码7-2#对数据的分布分析importpandasaspdimportmatplotlib.pyplotaspltdatafile='C:/Users/justaplayer/Documents/WeChatFiles/wxid_dcbvylvcfew......
  • python数据分析与挖掘实战第七章
    #代码7-1数据探索importpandasaspddatafile='data3/air_data.csv'#航空原始数据,第一行为属性标签resultfile='data3/explore.csv'#数据探索结果表data=......
  • 数据分析第七章
    航空公司客户价值分析数据探索importpandasaspddatafile=r"D:\py_project\a_三下\air_data.csv"resultfile=r'D:\py_project\a_三下\explore.csv'data=pd.......