Python 数据分析练习2:用户消费数据分析

需求:加载数据,4个字段(用户ID、购买日期、购买产品数量、购买金额)分别命名为user_id、order_dt、order_product、order_amount,查看数据的统计描述,在原数据中添加一列表示月份。根据月份,统计每月用户花费的总金额、所有用户每月的产品购买量、所有用户每月消费总次数,统计每月的消费人数。统计每个用户第一次和最后一次消费的月份。

链接:https://pan.baidu.com/s/1o8c_tBbBZUKFHRmHOBpmmg
提取码:q123

----------------------------------------------------

 ----------------------------------------------------

 ----------------------------------------------------

----------------------------------------

----------------------------------------

----------------------------------------

import pandas as pd

df = pd.read_csv("CDNOW_master.txt", header=None, sep='\s+', names=["user_id", "order_dt", "order_product", "order_amount"])
df["order_dt"] = pd.to_datetime(df["order_dt"], format="%Y%m%d")
df.describe()
df["month"] = df["order_dt"].astype("datetime64[M]")
df_month = df.pivot_table(index="month", values=["order_amount", "order_product"], aggfunc="sum")
df_month_person_consume_amount = df.pivot_table(index=["user_id", "month"], values="order_product", aggfunc="sum")
df_month_person_consume_times = df.pivot_table(index=["user_id", "month"], values="order_product", aggfunc="count")
df_month_person = pd.concat([df_month_person_consume_amount["order_product"], df_month_person_consume_times["order_product"]], axis=1)
df_person_per_month = df.pivot_table(index="month", values="user_id", aggfunc="count")
df_first_month = df.pivot_table(index="user_id", values="month", aggfunc=["min", "max"])

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hinomoto Oniko

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值