gpt4 book ai didi

python - 查找之前不存在的 customer_id

转载 作者:行者123 更新时间:2023-12-04 13:33:35 28 4
gpt4 key购买 nike

我想过滤 customer_id's以前的数据中不存在的所有 new_customer_ids在 2020-01-10 上是新的,但在 2020-01-01 上不存在
main_df

date          customer_id   amount_spent 
2020-01-01 24 123
2020-01-10 24 145
2020-01-01 58 89
2020-01-10 58 67
2020-01-01 98 34
2020-01-10 99 86
2020-01-10 67 140
2020-01-10 32 321
2020-01-10 75 76
输出_df
new_customer_id  amount_spent 
32 321
75 76
67 140
我曾尝试在 Pandas 中使用 shift 功能,但这对我不起作用
编辑
df = pd.DataFrame([["2020-01-01",24,123],
["2020-01-10",24,145],
["2020-01-01",58,89],
["2020-01-10",58,67],
["2020-01-01",98,34],
["2020-01-10",98,86],
["2020-01-10",67,140],
["2020-01-10",32,321],
["2020-01-10",75,76]],columns = ["date","customer_id","amount_spent" ])

最佳答案

IIUC你可以获得customer_id存在于 2020-01-01 ,然后过滤掉它们:

s = df.loc[df["date"]=="2020-01-01", "customer_id"]

print (df[~df["customer_id"].isin(s)])

date customer_id amount_spent
5 2020-01-10 99 86
6 2020-01-10 67 140
7 2020-01-10 32 321
8 2020-01-10 75 76

关于python - 查找之前不存在的 customer_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63574143/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com