首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >滚动12个月统计每月显示的唯一身份证

滚动12个月统计每月显示的唯一身份证
EN

Stack Overflow用户
提问于 2020-05-16 06:52:18
回答 1查看 591关注 0票数 0

我是与交易数据,并希望得到过去12个月滚动活跃的客户基础,但在每月水平。

下面是我拥有的事务数据的一个示例- 交易数据

代码语言:javascript
运行
复制
Cust-ID Date-of-Purchase    Quantity    Customer-Tag

N01847  01-04-18    10  B2C
N01847  15-04-18    20  B2C
N01847  25-06-19    20  B2C
N01847  12-02-20    100 B2C
N01847  25-03-20    150 B2C
N02341  01-12-19    20  B2B
N02341  02-02-20    150 B2B
N02011  25-01-19    10  B2B
N01033  02-06-18    30  B2C
N01033  02-04-19    40  B2C
N01033  15-04-19    50  B2C
N01033  15-02-20    20  B2C
N01010  16-01-18    100 B2C
N01010  25-02-18    250 B2C
N01010  03-09-18    10  B2C
N01010  04-04-19    250 B2C
N01010  15-06-19    100 B2C
N01010  26-07-19    40  B2C
N01010  17-09-19    10  B2C
N01010  17-09-19    10  B2C
N01010  20-03-20    20  B2C
N09100  20-01-18    20  B2B
N09100  20-01-18    20  B2B
N09100  25-01-20    20  B2B
N09100  25-01-20    20  B2B
N09100  25-01-20    20  B2B   ֿ
N09100  25-01-20    20  B2B

下面是我期望从python-期望输出获得的信息。

代码语言:javascript
运行
复制
Month-Year  B2C-Rolling-past-12months-Active-Customers  Monthly-Active-Customers    Monthly-Active-Rate

201801  100230  25058   25.0%

201802  100524  25634   25.5%

201803  100810  25213   25.0%

201804  101253  25495   25.2%

201805  101351  25525   25.2%

201806  103210  25998   25.2%

201807  103678  26122   25.2%

201808  103977  26202   25.2%

201809  104512  26342   25.2%

201810  104624  26376   25.2%

201811  105479  26597   25.2%

201812  111256  28059   26.2%

201901  112247  28314   25.2%

201902  112947  28497   25.2%

201903  113508  28644   25.2%

201904  113857  28737   25.2%

201905  114572  28924   25.2%

201906  115443  29149   25.3%

201907  116056  29310   25.3%

201908  116528  29435   25.3%

201909  116971  29553   25.3%

201910  117647  29729   25.3%

201911  118492  29949   25.3%

201912  124095  31371   26.3%

202001  124895  31580   25.3%

202002  125653  31778   25.3%

202003  126320  31953   25.3%

我非常感谢对python (spyder)代码的任何帮助,这些代码将有助于获得所需的结果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-16 08:34:35

你可能想用熊猫,然后做一些类似的事情:

代码语言:javascript
运行
复制
df["Date-of-Purchase"] = pd.to_datetime(df["Date-of-Purchase"], dayfirst=True)
df["Month"] = df["Date-of-Purchase"].dt.month
df["Year"] = df["Date-of-Purchase"].dt.year
res = df.groupby(["Year", "Month"])["Cust-ID"].nunique()

这将给你一个月的独特客户的数量。假设您拥有所有月份的数据,现在可以使用rolling获得12个月的滚动总和(我在代码中使用了3个月以便于调试):

代码语言:javascript
运行
复制
monthly_customers = df.groupby(["Year", "Month"])["Cust-ID"].apply(lambda x: frozenset(x.values))
monthly_customers = monthly_customers.reset_index()

monthly_customers = pd.concat([monthly_customers] + [monthly_customers["Cust-ID"].shift(i) for i in range(1, 3)], axis ="columns")
monthly_customers.columns = ["Year", "Month"] + [ f"shift_{i}" for i in range(3) ]

def count_unique(row):
    total_set = frozenset()
    columns = [ f"shift_{i}" for i in range(3) ]
    for col in columns:
        if row.get(col) and type(row.get(col)) == frozenset:
            total_set = total_set | row.get(col)
    return len(total_set)

monthly_customers["N_month_count"] = monthly_customers.apply(count_unique, axis=1)
monthly_customers

如果您没有所有月份的数据,则需要填写缺少的几个月。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61833011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档