Reputation: 3272
Objective: Calculate cumulative revenue since 2020-01-01.
I have a python dictionary as shown below
data = [{"game_id":"Racing","user_id":"ABC123","amt":5,"date":"2020-01-01"},
{"game_id":"Racing","user_id":"ABC123","amt":1,"date":"2020-01-04"},
{"game_id":"Racing","user_id":"CDE123","amt":1,"date":"2020-01-04"},
{"game_id":"DH","user_id":"CDE123","amt":100,"date":"2020-01-03"},
{"game_id":"DH","user_id":"CDE456","amt":10,"date":"2020-01-02"},
{"game_id":"DH","user_id":"CDE789","amt":5,"date":"2020-01-02"},
{"game_id":"DH","user_id":"CDE456","amt":1,"date":"2020-01-03"},
{"game_id":"DH","user_id":"CDE456","amt":1,"date":"2020-01-03"}]
The same dictionary above looks like this as a table
game_id user_id amt activity date
'Racing', 'ABC123', 5, '2020-01-01'
'Racing', 'ABC123', 1, '2020-01-04'
'Racing', 'CDE123', 1, '2020-01-04'
'DH', 'CDE123', 100, '2020-01-03'
'DH', 'CDE456', 10, '2020-01-02'
'DH', ' CDE789', 5, '2020-01-02'
'DH', 'CDE456', 1, '2020-01-03'
'DH', 'CDE456', 1, '2020-01-03'
Age is calculated as the difference between transaction date and 2020-01-01. Total Payer count is number of payers in each game.
I'm trying to create a dataframe having the cumulative results for a each day from the day of first transaction to the next day of transaction. eg:for game_id Racing we start with an amount of 5 on 2020-01-01 so Age is 0. on 2020-01-02 the amount is still 5 because we don't have a transaction on that day. on 2020-01-03 the amount is 5. but on 2020-01-04 the amount is 7 because we have 2 transactions on this day.
Expected output
Game Age Cum_rev Total_unique_payers_per_game
Racing 0 5 2
Racing 1 5 2
Racing 2 5 2
Racing 3 7 2
DH 0 0 3
DH 1 15 3
DH 2 117 3
DH 3 117 3
How to use window functions in python like how we use in SQL. Is there any better approach to solve this problem?
Upvotes: 0
Views: 629
Reputation: 13437
Here the very complicated part is to fill dates. I used an apply but I'm not sure this is the best way
import pandas as pd
data = [{"game_id":"Racing","user_id":"ABC123","amt":5,"date":"2020-01-01"},
{"game_id":"Racing","user_id":"ABC123","amt":1,"date":"2020-01-04"},
{"game_id":"Racing","user_id":"CDE123","amt":1,"date":"2020-01-04"},
{"game_id":"DH","user_id":"CDE123","amt":100,"date":"2020-01-03"},
{"game_id":"DH","user_id":"CDE456","amt":10,"date":"2020-01-02"},
{"game_id":"DH","user_id":"CDE789","amt":5,"date":"2020-01-02"},
{"game_id":"DH","user_id":"CDE456","amt":1,"date":"2020-01-03"},
{"game_id":"DH","user_id":"CDE456","amt":1,"date":"2020-01-03"}]
df = pd.DataFrame(data)
# we want datetime not object
df["date"] = df["date"].astype("M8[us]")
# we will need to merge this at the end
grp = df.groupby("game_id")['user_id']\
.nunique()\
.reset_index(name="Total_unique_payers_per_game")
# sum amt per game_id date
df = df.groupby(["game_id", "date"])["amt"].sum().reset_index()
# dates from 2020-01-01 till the max date in df
dates = pd.DataFrame({"date": pd.date_range("2020-01-01", df["date"].max())})
# add missing dates
def expand_dates(x):
x = pd.merge(dates, x.drop("game_id", axis=1), how="left")
x["amt"] = x["amt"].fillna(0)
return x
df = df.groupby("game_id")\
.apply(expand_dates)\
.reset_index().drop("level_1", axis=1)
df["Cum_rev"] = df.groupby("game_id")['amt'].transform("cumsum")
# this is equivalent as long as data is sorted
# df["Cum_rev"] = df.groupby("game_id")['amt'].cumsum()
# merge unique payers per game
df = pd.merge(df, grp, how="left")
# dates difference
df["Age"] = "2020-01-01"
df["Age"] = df["Age"].astype("M8[us]")
df["Age"] = (df["date"]-df["Age"]).dt.days
# then you can eventually filter
df = df[["game_id", "Age",
"Cum_rev", "Total_unique_payers_per_game"]]\
.rename(columns={"game_id":"Game"})
Upvotes: 1