博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python - 【用户、商品】【购买、浏览】数据处理
阅读量:1984 次
发布时间:2019-04-27

本文共 3034 字,大约阅读时间需要 10 分钟。

import pandas as pdclos = ['Time', 'Action', 'User', 'Product', 'Quantity', 'Price']orders = pd.read_csv('Data/purchase_order.tab', sep='\t',parse_dates={'Dates' : [0]}, names=clos, encoding='utf-8')orders.info()

RangeIndex: 54772 entries, 0 to 54771Data columns (total 6 columns):Dates 54772 non-null datetime64[ns]Action 54772 non-null objectUser 54772 non-null objectProduct 54772 non-null objectQuantity 54772 non-null int64Price 54721 non-null float64dtypes: datetime64[ns](1), float64(1), int64(1), object(3)memory usage: 2.5+ MB
orders.head()

# 求某个商品的总售出量orders[orders['Product'] == 'P0002267974']['Price'].describe()

count     10.0mean     285.0std        0.0min      285.025%      285.050%      285.075%      285.0max      285.0Name: Price, dtype: float64

# 商品去重orders['Product'].unique()

array(['P0006944501', 'P0006018073', 'P0002267974', ..., 'P0022884606',       'P0013911085', 'P0025123755'], dtype=object)
# 商品单价orders.groupby('Product')['Price'].mean().sort_values(ascending=False).head()

ProductP0000143511    438888.0P0000143500    438888.0P0006584093    320000.0P0025280275    183900.0P0000150006    111375.0Name: Price, dtype: float64
# 增加总价格栏位orders['Total_Price'] = orders['Quantity'] * orders['Price']orders.head()

# 查看哪个用户今天消费最高orders.groupby('User')['Total_Price'].sum().sort_values(ascending=False).head()
UserU166708333      2942744.0U10120098943    1451117.0U142809250       747550.0U1006283751      515688.0U10114715330     456782.0Name: Total_Price, dtype: float64
import pandas as pdclos = ['Time', 'Action', 'User', 'Product']views = pd.read_csv('Data/purchase_view.tab', sep='\t',parse_dates={'Dates' : [0]}, names=clos, encoding='utf-8')views.info()

# 查看用户购买记录clos = ['Time', 'Action', 'User', 'Product', 'Quantity', 'Price']orders = pd.read_csv('Data/purchase_order.tab', sep='\t',parse_dates={'Dates' : [0]}, names=clos, encoding='utf-8') # reset_index name column,DataFrameorders_cnt = orders.groupby(['User', 'Product'])['Product'].count().reset_index(name='buys')orders_cnt.head()

# 查看用户浏览记录views_cnt = views.groupby(['User', 'Product'])['Product'].count().reset_index(name='views')views_cnt.head()

# 合并购买与浏览记录merge_df = pd.merge(orders_cnt, views_cnt, on=['User', 'Product'], how='right')merge_df.head()

%pylab inlineviews_cnt_by_date.plot(kind='line', figsize=[10, 5])

# 根据小时计算用户的浏览记录总和views_cnt_by_hour = views.groupby(views['Dates'].dt.hour)['Action'].count()views_cnt_by_hour.plot(kind='line', figsize=[10, 5])

# 查看消费大户orders['Total_Price'] = orders['Quantity'] * orders['Price']g = orders.groupby('User')['Total_Price'].sum().sort_values(ascending=False)[0:10]g.plot(kind='bar', figsize=[10, 5])

# merge 用户每日浏览量和购买量views_daily_cnt = views.groupby(by=views['Dates'].dt.date)['Action'].count()orders_daily_cnt = orders.groupby(by=orders['Dates'].dt.date)['Action'].count()df = pd.concat([views_daily_cnt, orders_daily_cnt], axis=1) #用index 做merge keydf.dropna(inplace=True)df.columns = ['views', 'orders']df.plot(kind='line', figsize=[10, 5], rot=45)

你可能感兴趣的文章
python2.6.x/python3发送邮件,并在正文中显示附件中的图片
查看>>
Dubbo服务治理向SpringCloud服务治理兼容,过渡
查看>>
JAVA使用HBase的Rowkey精确批量处理
查看>>
Collections排序sort排序list,单个及多条件排序
查看>>
Mysql中where 条件中加 if 判断-纯jdbc
查看>>
分布式数据中间件TDDL、Amoeba、Cobar、MyCAT架构比较
查看>>
使用tk.mapper mybatis 插件注意点时对于实体类中某字段不是表中字段,处理方式
查看>>
Sharding-JDBC的SQL引擎(Druid)处理的支持情况总结
查看>>
Apache Kafka:优化部署的 10 种最佳实践
查看>>
HBase 中加盐之后的表如何读取:Spark 篇
查看>>
一篇文章了解 Spark Shuffle 内存使用
查看>>
【免费下载】某平台3980元Hadoop大数据/机器学习全套视频,仅此1次
查看>>
Apache Hive 联邦查询(Query Federation)
查看>>
为什么说流处理即未来?
查看>>
Leetcode 剑指 Offer 39. 数组中出现次数超过一半的数字 c#
查看>>
Leetcode 35. 搜索插入位置 c#
查看>>
LeetCode64:最小路径和
查看>>
LeetCode931. 下降路径最小和
查看>>
LeetCode62. 不同路径
查看>>
记gdb调试一次报错:Missing separate debuginfos, use: zypper install glibc-32bit-debuginfo-2.22-15.3.x86_64
查看>>