Skip to content

Commit

Permalink
Do a day-of-week donation analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
davepeck committed Dec 4, 2023
1 parent 7e2383c commit 89cda2c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
Binary file added data/.DS_Store
Binary file not shown.
83 changes: 78 additions & 5 deletions notebooks/when-donate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -36,14 +36,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 70659611/70659611 [01:17<00:00, 914596.13it/s] \n"
"100%|██████████| 70659611/70659611 [01:16<00:00, 922617.11it/s] \n"
]
}
],
Expand Down Expand Up @@ -85,7 +85,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -124,7 +124,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -185,6 +185,79 @@
"ax.yaxis.set_major_formatter(mtick.StrMethodFormatter(\"${x:,.0f}\"))\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There seems to be a very strong day-of-week behavior in this data. What day of the week tends to get the most donations? What day of the week gets the least?"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total contribution amount and number of contributors by day of week:\n",
"Day of week: Tuesday\n",
"Total amount: $203,150,766\n",
"Number of contributors: 953,760\n",
"\n",
"Day of week: Sunday\n",
"Total amount: $177,424,293\n",
"Number of contributors: 817,579\n",
"\n",
"Day of week: Monday\n",
"Total amount: $176,489,050\n",
"Number of contributors: 909,178\n",
"\n",
"Day of week: Wednesday\n",
"Total amount: $166,863,743\n",
"Number of contributors: 812,352\n",
"\n",
"Day of week: Thursday\n",
"Total amount: $149,479,020\n",
"Number of contributors: 702,626\n",
"\n",
"Day of week: Friday\n",
"Total amount: $83,420,324\n",
"Number of contributors: 664,096\n",
"\n",
"Day of week: Saturday\n",
"Total amount: $83,191,962\n",
"Number of contributors: 629,041\n",
"\n"
]
}
],
"source": [
"# For each of the seven days of the week, compute the average contribution amount and number of contributors\n",
"import datetime\n",
"from collections import defaultdict\n",
"\n",
"contributions_by_day_of_week: dict[int, tuple[float, int]] = defaultdict(lambda: (0.0, 0))\n",
"for date, amount_usd in contributions:\n",
" day_of_week = date.weekday()\n",
" total_amount_usd, num_contributors = contributions_by_day_of_week[day_of_week]\n",
" total_amount_usd += amount_usd\n",
" num_contributors += 1\n",
" contributions_by_day_of_week[day_of_week] = (total_amount_usd, num_contributors)\n",
"\n",
"# Sort by total amount\n",
"contributions_by_day_of_week = dict(sorted(contributions_by_day_of_week.items(), key=lambda x: x[1][0], reverse=True))\n",
"\n",
"# Print the results\n",
"print(\"Total contribution amount and number of contributors by day of week:\")\n",
"for day_of_week, (total_amount_usd, num_contributors) in contributions_by_day_of_week.items():\n",
" print(f\"Day of week: {datetime.date(2020, 1, 5 + day_of_week).strftime('%A')}\")\n",
" print(f\"Total amount: ${total_amount_usd:,.0f}\")\n",
" print(f\"Number of contributors: {num_contributors:,}\")\n",
" print()\n"
]
}
],
"metadata": {
Expand Down

0 comments on commit 89cda2c

Please sign in to comment.