Python Forum
Analyzing yearly expenses (CSV file) from debit card
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Analyzing yearly expenses (CSV file) from debit card
#1
Hello,

I am quite new with programming in Python and now I have started my (kind of) 'first' project in Python. I want to analyse my yearly expenses and I think pandas is the best way to give a clear overview with a chart.

The query:
  • I have a yearly overview of expenses in a CSV file (see attached). This is an example, not my 'real' expenses Tongue
  • The lay-out of the csv file ins't clear, so I want to change it. Preferred is have all the columns 'Alligned left' and the first row (headers) should be Bold.
  • The output of the chart should visualize my expenses monthly. In the end I want to have an overview in which month the expenses are high and in which months the expenses are relative low.
  • If the analysis is done and the chart is made. I want to save the file with _MODIFIED added to the original file.

I tried starting this project with open and edit a CSV file using the following code:
import csv
from pathlib import Path
from tkinter.filedialog import askopenfilename
import sys
 
 
def read_csv_file(filename):
    parts = list(filename.parts)
    parts[-1] = f"{filename.stem}Modified{filename.suffix}"
    parts[0] = ''
    outfilename = Path(f"{'/'.join(parts)}")
 
    print(f"new output file name: {outfilename}")
 
    with filename.open() as fp, outfilename.open('w') as fout:
        crdr = csv.reader(fp, delimiter=',')
        cwrtr = csv.writer(fout, delimiter=',')
        for row in crdr:
            print(row)
            # Modify row as desired here
            cwrtr.writerow(row)
 
def get_filename():
    badcount = 0
 
    while(True):
        try:
            if badcount > 2:
                print("Three strikes and your out!")
                sys.exit(-1)
            filename = Path(askopenfilename(filetypes=[("CSV files","*.csv")]))
            break
        except TypeError:
            badcount += 1
            print(f"bad filename, try again")
         
    return filename
 
 
if __name__ == '__main__':
    read_csv_file(get_filename())
I have tried to code this project in Python, but after three weeks I just have 10 lines of code..... I just don't know where to start I guess.

CSV file:https://ibb.co/5rqR1YQ
I couldn't upload a .csv file, so instead I have added the picture above.

The amounts of the expenses are examples, so I haven't spent €986,- euros at the swimming pool Big Grin

Hopefully someone can help!
Larz60+ write Dec-13-2020, 12:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

You should avoid using links as many members will not follow.
Reply
#2
Are you trying to build a pivot table? pandas has support for them, though I must admit I haven't use it before.

Formatting cells (e.g. changing fonts to bold) , assuming you are using Excel to display the csv, can be done with openpyxl.
Reply
#3
(Dec-13-2020, 02:29 PM)palladium Wrote: Are you trying to build a pivot table? pandas has support for them, though I must admit I haven't use it before.

Formatting cells (e.g. changing fonts to bold) , assuming you are using Excel to display the csv, can be done with openpyxl.

I am not very familiar with pivot tables, I was assuming a chart was reasonable for this kind of demanding.

Yes I am using Excel to display the csv file. Openpyxl is only for reading Excel xlsx, xlsm, xltx and xltm files I thought?
Reply
#4
(Dec-13-2020, 02:42 PM)Python_User Wrote: Yes I am using Excel to display the csv file. Openpyxl is only for reading Excel xlsx, xlsm, xltx and xltm files I thought?

It can do formatting too:
https://openpyxl.readthedocs.io/en/stable/styles.html

(Dec-13-2020, 02:42 PM)Python_User Wrote: I am not very familiar with pivot tables, I was assuming a chart was reasonable for this kind of demanding.

Openpyxl does charts too: https://openpyxl.readthedocs.i/oen/stabl...ction.html

I must say I haven't used any of these features myself, I just came across these whilst reading the docs.
Python_User likes this post
Reply
#5
Quote:It can do formatting too:
https://openpyxl.readthedocs.io/en/stable/styles.html

Quote:Openpyxl does charts too: https://openpyxl.readthedocs.i/oen/stabl...ction.html

I must say I haven't used any of these features myself, I just came across these whilst reading the docs.

Aha, I think I got some homework to do this weekend ;).

Thank you palladium for the links, I will dive into them during this weekend.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  create 10 yearly blocks from time series using pandas Staph 1 1,933 Jul-23-2019, 12:01 PM
Last Post: Malt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020