题解 | #筛选某店铺最有价值用户中消费最多前5名#

筛选某店铺最有价值用户中消费最多前5名

https://www.nowcoder.com/practice/58655010a7c34e9fb2b7b491c3f79ca4

import pandas as pd
import numpy as np

pd.set_option("display.max_columns", None)  # 显示所有的列,而不是以……显示
pd.set_option("display.max_rows", None)  # 显示所有的行,而不是以……显示
pd.set_option("display.width", None)  # 不自动换行显示
df = pd.read_csv("sales.csv")


def sales_score(sales):
    """
    为销售数据计算评分

    Parameters:
        sales (pandas.DataFrame): 销售数据

    Returns:
        pandas.DataFrame: 包含每个观测值各项得分的数据帧
    """
    df_down = sales.quantile(0.25)
    df_mid = sales.quantile(0.5)
    df_up = sales.quantile(0.75)

    # R_Quartile: recency 数据评分
    q = pd.qcut(sales["recency"], 4, labels=False)
    R_Quartile = pd.cut(
        sales.recency,
        [np.NINF, df_down["recency"], df_mid["recency"], df_up["recency"], np.Inf],
        labels=[4, 3, 2, 1],
    ).astype("int")

    # F_Quartile: frequency 数据评分(与 recency 数据评分取反)
    F_Quartile = pd.cut(
        sales.frequency,
        [
            np.NINF,
            df_down["frequency"],
            df_mid["frequency"],
            df_up["frequency"],
            np.Inf,
        ],
        labels=[1, 2, 3, 4],
    ).astype("int")
    F_Quartile = 5 - F_Quartile

    # M_Quartile: monetary 数据评分(与 recency 数据评分取反)
    M_Quartile = pd.cut(
        sales.monetary,
        [np.NINF, df_down["monetary"], df_mid["monetary"], df_up["monetary"], np.Inf],
        labels=[1, 2, 3, 4],
    ).astype("int")
    M_Quartile = 5 - M_Quartile

    # 将三列评分添加到数据帧中
    sales["R_Quartile"] = R_Quartile
    sales["F_Quartile"] = F_Quartile
    sales["M_Quartile"] = M_Quartile
    sales["RFMClass"] = (
        sales["R_Quartile"].map(str)
        + sales["F_Quartile"].map(str)
        + sales["M_Quartile"].map(str)
    )

    return sales


sales = sales_score(df)

print(sales.loc[:, ["user_id", "recency", "frequency", "monetary", "RFMClass"]].head(5))

print(
    sales.query("RFMClass == '444'")
    .sort_values(by="monetary", ascending=False)
    .loc[:, ["user_id", "recency", "frequency", "monetary", "RFMClass"]]
    .head(5)
)

全部评论

相关推荐

10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务