Skip to content

Instantly share code, notes, and snippets.

Last active June 6, 2020 08:46
What would you like to do?
def add_day_month_year(df):
dates = df["Date"].map(lambda x: x[:10])
df["day_of_month"] = x: x.split("/")[1])
df["month"] = x: x.split("/")[0])
df["year"] = x: x.split("/")[2])
df["date"] = dates
return df
from pandas import read_csv
crimes_df = read_csv("CrimeData.csv")
crimes_df = add_day_month_year(crimes_df)
crimes_df = crimes_df[crimes_df.year <= "2019"]
crimes_count_df = crimes_df \
.groupby(["year", "month"]) \
.size() \
.to_frame("crimes") \
from calendar import monthrange
crimes_count_df["Days in month"] = crimes_count_df.apply(lambda row: monthrange(int(row.year), int(row.month))[1], axis=1)
crimes_count_df["Average crimes"] = crimes_count_df.apply(lambda row: row["crimes"] / row["Days in month"], axis=1)
df = crimes_count_df \
.groupby(["month"])["Average crimes"] \
.mean() \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment