+++ title = "Credit Card Spending Dashboards" date = "2020-06-16" +++
import pandas as pd
import gspread
import matplotlib.pyplot as plt
gc = gspread.service_account()
sh = gc.open_by_key("1_tbwz6Z9uVZMJQZTysSQJYbSg-LCRdk6u49UngChM-Y")
worksheet = sh.worksheet("Responses Raw")
dataframe = pd.DataFrame(worksheet.get_all_records())
dataframe["Incurring Month"] = pd.to_datetime(dataframe["Incurring Date"]).dt.strftime(
"%Y%m"
)
df_filtered = dataframe[~dataframe["Category"].isin(["Fake", "Travel"])]
df_by_monthly = pd.pivot_table(
df_filtered,
index="Incurring Month",
columns="Category",
values="Amount of Money",
aggfunc=sum,
)
df_by_monthly.fillna(0)
df_by_monthly_plot = df_by_monthly.plot.bar(stacked=True, figsize=(16, 9))
df_by_monthly_plot.set_ylabel("Total Amount ($)")
plt.xticks(rotation=45);
df_travel = dataframe[
(dataframe["Category"] == "Travel") & (dataframe["Travel Tag"] != "")
]
df_travel_by_tag = pd.pivot_table(
df_travel,
index="Travel Tag",
columns="Travel Details",
values="Amount of Money",
aggfunc=sum,
margins=True,
)
df_travel_by_tag = (
df_travel_by_tag.sort_values(["All"], ascending=False)
.drop("All", axis=1)
.drop("All")
)
df_travel_by_tag.fillna(0)
df_travel_by_tag_plot = df_travel_by_tag.plot.bar(stacked=True, figsize=(12, 8))
df_travel_by_tag_plot.set_ylabel("Total Amount ($)")
plt.xticks(rotation=45);