Let's visualize the consumption of my home. Only what I could do at the Python beginner level. Since it is a trial and error process, the details are appropriate.
Just prepare two csv data. It will be as follows. Date, type, and cost (enter your daily consumption here). Here I am writing the data dates in order. If you write in a mess, do you need to devise something like trying to make it in order? A description of the type and content. (Corresponds to the above csv data type.)
First, install the library you want to use. Then read the csv data and combine them. And the cumulative cost and the month of the data date are added to the data frame.
import pandas as pd
import datetime
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot') #Magic
font = {'family' : 'meiryo'}
df = pd.read_csv('kakeibo.csv')
abcde = pd.read_csv('kakei.csv') #Type contents list csv
df = pd.merge(df, abcde, on="type", how="left") #Data combination
df['Accumulation'] = np.cumsum(df['cost'])
df["Data date"] = pd.to_datetime(df["Data date"])
df["Month"] = df["Data date"].dt.strftime("%Y%m")
df.head()
Shows the cumulative total for each date. Then visualize it in a graph. (Since it is test data, the content is appropriate. As you can see the csv data, it is 5/14 after 4/19, so the graph looks like this.)
df_sum = df.groupby("Data date").sum()[["Accumulation"]] #Aggregate by date
df_sum.head()
df_sum.plot(y='Accumulation',figsize=(20,6))
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
plt.xlabel("date")
plt.ylabel("cost")
Finally, create the cost for each content and visualize it with a pie chart.
df_new = df.groupby("Contents").sum()[["cost"]] #集計 日付とContentsごと
df_new.plot(kind='pie',x='Contents', y = 'cost' ,counterclock=False, startangle=90, autopct="%1.1f%%", pctdistance=0.7,figsize=(20,6) )
It was a beginner level play. As I wrote at the beginning, it is a trial and error process. I think there are many parts that can be expanded and improved. In any case, it will be the actual production after increasing the amount of original data to about one month.
Recommended Posts