Skip to content

Instantly share code, notes, and snippets.

@ericgitonga
Last active June 24, 2021 06:35
Show Gist options
  • Save ericgitonga/9cf50fdeb92d077257e0770b6eaaf5a9 to your computer and use it in GitHub Desktop.
Save ericgitonga/9cf50fdeb92d077257e0770b6eaaf5a9 to your computer and use it in GitHub Desktop.
df["days_in_month"] = df.index.daysinmonth
df_average = df.groupby(["year","month","month_number","days_in_month"])["funded_amount"].sum().reset_index()
df_average["monthly_average"] = round(df_average["funded_amount"] / df_average["days_in_month"],2)
sorted_df_average = []
for year in list(df_average["year"].unique()):
sorted_df_average.append(df_average[df_average["year"] == year].sort_values(by="month_number"))
df_average = pd.concat(sorted_df_average).reset_index(drop=True).\
drop(["month_number","days_in_month","funded_amount"],"columns")
df_average
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment