[Let's play with Python] Make a household account book

Introduction

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.

Preparation

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? 2020-04-19.png A description of the type and content. (Corresponds to the above csv data type.) 2020-04-19 (1).png

Python code

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()

2020-04-19 (2).png

Visualization with line graph

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")

2020-04-19 (3).png

Visualize with a pie chart

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) )

2020-04-19 (4).png

in conclusion

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

[Let's play with Python] Make a household account book
Let's make a GUI with python.
Let's make a graph with python! !!
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Let's replace UWSC with Python (5) Let's make a Robot
Make a fortune with Python
I made a household account book bot with LINE Bot
Let's make a simple game with Python 3 and iPhone
[For play] Let's make Yubaba a LINE Bot (Python)
[Piyopiyokai # 1] Let's play with Lambda: Get a Twitter account
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
[Super easy] Let's make a LINE BOT with Python.
Let's make a breakout with wxPython
Let's play with Excel with Python [Beginner]
Make a recommender system with python
Let's make a supercomputer with xCAT
Let's make a websocket client with Python. (Access token authentication)
Let's make a WEB application for phone book with flask Part 1
Let's create a free group with Python
Let's make a WEB application for phone book with flask Part 2
Fractal to make and play with Python
Play with 2016-Python
Let's make a simple language with PLY 1
[Python] Let's make matplotlib compatible with Japanese
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a combination calculation in Python
Let's make a web chat using WebSocket with AWS serverless (Python)!
Make a desktop app with Python with Electron
[Ev3dev] Let's make a remote control program by Python with RPyC protocol
Python hand play (let's get started with AtCoder?)
[Piyopiyokai # 1] Let's play with Lambda: Creating a Lambda function
[Python] Make a game with Pyxel-Use an editor-
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
[Python] Make a simple maze game with Pyxel
Try to make a dihedral group with Python
Let's make a module for Python using SWIG
If you want to make a discord bot with python, let's use a framework
Try to make a command standby tool with python
[Practice] Make a Watson app with Python! # 2 [Translation function]
Make a simple Slackbot with interactive button in python
Make a breakpoint on the c layer with python
Let's make dependency management with pip a little easier
Make a CSV formatting tool with Python Pandas PyInstaller
Let's make a Mac app with Tkinter and py2app
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython
What is God? Make a simple chatbot with python
[Python] Road to a snake charmer (5) Play with Matplotlib
Let's play with 4D 4th
Let's make a Discord Bot.
Let's play with Amedas data-Part 1
Let's run Excel with Python
Let's make Othello with wxPython
Let's play with Amedas data-Part 4
Make Puyo Puyo AI with Python