数据分析处理(三)

前十个花钱最多的用户ID

jupyter导包

import pandas as pd  #导入pandas用于表格操作
import xlrd  #导入xlrd用于获取一个表格里多个sheet
from matplotlib import pyplot as plt#导入pyplot用于绘图

解决中文乱码问题

#sans-serif就是无衬线字体,是一种通用字体族。
#常见的无衬线字体有 Trebuchet MS, Tahoma, Verdana, Arial, Helvetica, 中文的幼圆、隶书等等。
import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['SimHei'] #指定默认字体 SimHei为黑体
mpl.rcParams['axes.unicode_minus']=False #用来正常显示负号

读取表格合并sheet(当知道sheet名时)

table1=pd.read_excel('meal_order_detail.xlsx',sheet_name='meal_order_detail1')
table2=pd.read_excel('meal_order_detail.xlsx',sheet_name='meal_order_detail2')
table3=pd.read_excel('meal_order_detail.xlsx',sheet_name='meal_order_detail3')
foodData=pd.concat([table1,table2,table3],axis=0,sort=False)
foodData

去重ID

foodData['total_price']=foodData['counts']*foodData['amounts']
foodData2=foodData.drop_duplicates(['order_id'])
print(foodData2)

取出去重后的ID换成一个列表

order_id1=foodData2['order_id'].values  #它是一个array类型
print(order_id1)
order_id_list=order_id1.tolist() #转成List

1.拆:得到每一个id对应价钱 2.组:价钱组成新的列表

price_list=[]
for i in order_id_list:  #遍历id,
    moneys=foodData[foodData['order_id']==i]['total_price'] #得到每一个id即i对应的数据中再去取价钱
    print(moneys)

    user_sum_price=np.sum(moneys.values) #取出它们的值为array类型并用numpy中sum方法求和。
    price_list.append(user_sum_price) #然后把这些求和的价钱再添加到新的列表里

3.组:将价格列表和去重后的id列表组成一个DataFrame类型进行排序

pd_user_price=pd.DataFrame(price_list,index=order_id_list)  
pd_user_price.sort_values(0,ascending=False,inplace=True) #降序
pd_user_price

取出0字段的内容

hand_price=pd_user_price[0] 
print(hand_price)

结果:
1166 1314
743 1214
1317 1210
576 1162
408 1148

1135 79
1320 78
1256 77
874 70
1064 48
Name: 0, Length: 942, dtype: int64
绘图

X2=hand_price.index[0:10].tolist()  #取X
Y2=hand_price.values[0:10].tolist() #取Y
plt.title('花钱最多的前十用户')
plt.xlabel('用户ID') #定义X坐标轴
plt.ylabel('消费') #定义Y坐标轴
plt.xticks(np.arange(10),X2,rotation=60) #定义xticks
plt.bar(['a','b','c','d','e','f','g','h','i','j'],Y2)  #先自定义一个X坐标轴,直接给X2不能正常显示,然后通过xticks对X轴的设置进行X轴的赋值
plt.show()

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值