我有一个问题。我想计算第二天有多少商品到达客户手中。这意味着,例如,我有 customerId == 1
的客户,我想查看当天 2022-05-04
以了解第二天有多少包裹到达。第二天是 2022-05-05
。如果我们将客户的两天加起来,我们会得到 2 天。
最后日期不应包含值,例如 2022-05-08 == None
。
我尝试计算下一个日期。但是我如何计算第二天到达的元素数量呢?
数据框:
customerId fromDate
0 1 2022-05-04
1 1 2022-05-05
2 1 2022-05-05
3 1 2022-05-06
4 1 2022-05-08
5 2 2022-05-10
6 2 2022-05-12
代码:
import pandas as pd
import datetime
d = {'customerId': [1, 1, 1, 1, 1, 2, 2],
'fromDate': ['2022-05-04', '2022-05-05', '2022-05-05', '2022-05-06', '2022-05-08', '2022-05-10', '2022-05-12']
}
df = pd.DataFrame(data=d)
def nearest(items, pivot):
try:
return min(items, key=lambda x: abs(x - pivot))
except:
return None
df['fromDate'] = pd.to_datetime(df['fromDate'], errors='coerce').dt.date
df["count_next_date"] = df['fromDate'].apply(lambda x: nearest(df['fromDate'], x))
[OUT]
customerId fromDate count_next
0 1 2022-05-04 2022-05-04
1 1 2022-05-05 2022-05-05
2 1 2022-05-05 2022-05-05
3 1 2022-05-07 2022-05-07
4 2 2022-05-10 2022-05-10
5 2 2022-05-12 2022-05-12
我想要什么:
customerId fromDate count_next
0 1 2022-05-04 2
1 1 2022-05-05 1
2 1 2022-05-05 1
3 1 2022-05-06 0
4 1 2022-05-08 None
5 2 2022-05-10 0
6 2 2022-05-12 None
最佳答案
带注释的代码
# Convert the column to datetime
df['fromDate'] = pd.to_datetime(df['fromDate'])
# Group by custid and prev date to calculate
# number of items arriving next day
date = df['fromDate'] - pd.DateOffset(days=1)
items = df.groupby(['customerId', date], as_index=False).size()
# Merge the item count with original df
out = df.merge(items, how='left')
# Fill the nan values with 0
out['size'] = out['size'].fillna(0)
# mask the item count corresponding to last date for each customerid
out['size'] = out['size'].mask(~out['customerId'].duplicated(keep='last'))
结果
print(out)
customerId fromDate size
0 1 2022-05-04 2.0
1 1 2022-05-05 1.0
2 1 2022-05-05 1.0
3 1 2022-05-06 0.0
4 1 2022-05-08 NaN
5 2 2022-05-10 0.0
6 2 2022-05-12 NaN
关于python - 随着时间的推移计算第二天有多少元素到达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72416085/