Created
February 14, 2020 09:30
-
-
Save yassineAlouini/2ade5c8b1431231ad411a8bf1b94e379 to your computer and use it in GitHub Desktop.
Compute the variations of the total column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_input_df(): | |
df = pd.DataFrame({"date": pd.date_range("2017-1-1", "2020-1-1", freq="1D")}) | |
df["open"] = np.random.choice([1, 0], len(df)) | |
df["closed"] = np.random.choice([1, 0], len(df)) | |
df["location"] = np.random.choice(["a", "b", "c"], len(df)) | |
return df | |
def compute_total_variations_df(df): | |
df = df.copy() | |
df["total"] = (df["open"] - df["closed"]) | |
df = df.set_index("date").groupby("location").resample("1M")["total"].sum().reset_index() | |
df["total"] = df.groupby(["date", "location"])["total"].transform("cumsum") | |
dfs = [] | |
for location in df["location"].unique(): | |
_df = df.loc[lambda df: df["location"] == location].sort_values("date") | |
last_year_total_s = _df["total"].shift(12) | |
_df["variation_total"] = 100 * ((_df["total"] - last_year_total_s ) / last_year_total_s) | |
dfs.append(_df) | |
return pd.concat(dfs) | |
result_df = get_input_df().pipe(compute_total_variations_df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment