Master Python With This ONE Project!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're looking to practice your python skills then you are in the right place today I'm going to be walking you through a python project that really will help you master the language not only does it cover a lot of the syntax in Python and some of the advanced features but it utilizes some very popular python modules so you can understand how to use this language to build a real practical project we're going to be building a personal finance tracker with the ability to track and log different transactions organize them get summaries of income and expenses and even view our data in a graph so we can see how much money we're making month over month this is a great project and let me show you a quick demo so you can see exactly what we'll build all right so I'm on the computer now and we'll start with a quick demo of the project so you can see here that we have a few different options we can add a new transaction or we can view transactions in kind of a summary within some range or we can exit so let's add a new transaction to start we're going to enter the date uh let's go with maybe 207 2023 let's enter the amount of the transaction $124 and then we have to classify if this is an income or expense so let's say this is an expense and then I can give this a description and maybe I'll say this erir groceries okay perfect now we've entered that transaction now what I can do is view some transactions and a summary within a range so I'm going to go with two now let's enter the start date let's go with maybe 01 07 20 24 and let's go with 20 07 2024 okay when I do that you can see that it gives me all of the different income and expenses that I have within that date range gives me a quick summary with the total income total expenses then the net savings or the net profit then I actually have the option to view this as a plot so I'm going to go ahead and press yes and you can see now that this actually opens up a chart and shows me all of the income and expenses over that date range now let's go back and I just want to show you that all of this data is stored within a CSV file so this could easily be imported into something like Excel or Google Sheets if you wanted to and this is kind of what we're using as the database for the project so you can see we have the date amount category and description obviously you can extend this and make it a lot more complex but this is a really great stter project and you're going to learn a significant amount from it now that is the project that will help you master the Python language just like I've mastered the segue to our sponsor i.net i.net is a decentralized cloud GPU Network that allows dormant gpus to be used by consumers if you have gpus that you're not using i.net provides a marketplace where consumers can utilize that compute and you can get paid i.net is based on the salana network and the io token its aim is to create the largest network of geod distributed gpus this network allows machine learning Engineers to access scalable distributed computer clusters at a fraction of the cost of centralized Services i.net is uniquely capable of creating clusters of tens of thousands of gpus whether they are collocated or Geo distributed whether you need compute power or have unused Hardware you can take advantage of this great Marketplace from i.net anyone can provide compute and at the moment i.net has over 148,000 verified gpus and over 43,000 verified CPUs available to use with the recent hype in the AI field more and more Engineers need this type of compute and i.net is providing it to them check out this great project by clicking the link in the description and now let's get into the video so let's go ahead and get started with this project now what I've done is created a new folder called personal finance tracker tutorial on my desktop and just open this up in Visual Studio code you can work in any editor or IDE that you want and I'm assuming for this project that you have some familiarity with python and you know how to for example open a new project create a folder Etc so just get a folder ready for writing this code now what we're going to do is install the different modules that we'll need for this project specifically we're going to need map plot lib and pandas these are the two modules map plot label will allow us to plot and see the graph and pandas will allow us to more easily kind of categorize and search for data within our CSV file so in order to install these we're going to type the following command that's pip install and then matap plot lib this is going to be in your terminal or your command prompt now if you're on Mac or Linux this will be pip three install map plot lib and after we install that I already have it installed so I'm just going to skip this we're going to install pandas now in case you get lost at any point in this tutorial all of the code will be available from the link in the description as well as a requirements file that specifies the few different modules that you need okay so at this point we should be good to go we have map plot lib and pandas and now we're going to create some files so the first file we're going to create here can just be main.py and I'm going to make another file called dataor entry. piy this is because I want to have one file that's going to kind of collect data from the user so it'll have all of our input statements it'll be validating different data and then a main file which will handle kind of the main flow of the program now for this program we're going to have a CSV file again comma separated values and that's going to act as our database so I want to begin by working with the CSV file so initializing it creating if it creating it s if it doesn't already exist and then looking at how we can add new entries into it and read data from it so let's start there after we work with the CSV file we're then going to have to be able to view different transactions so we can get kind of that summary list that we saw before we're also going to have to give some nice input or kind of a nice view to the user so they have the ability to interact with it from the terminal so a few different steps here but let's start with the CSV file so at the top of our program we're going to say import pandas as PD again pandas is going to allow us to actually load in the CSV file and work with it a lot easier we're then going to import the CSV module pretty straightforward what that will do and I'm going to say from date time import date time this is a module built into python which is going to help us work with dates and times which we're going to need for this project now what I'm going to do is I'm going to start by setting up a class which is going to have a few different methods that allow us to work really easily with our CSV file so I'm going to say class CSV and then inside of the class I'm going to create a variable I'm going to say CSV file is equal to and I'm going to give a name to the file we want to work with so I'm going to call this Finance data.csv V this is what's known as a class variable It's associated with the class and that makes sense because we're going to be using it just within this class which has some functions related to the CSV file now you don't need to make a class for this but I'm just trying to show you some more advanced python features hence why I'm doing this okay so the first thing we need to do is we need to initialize the CSV file that means that we need to read it in if it already exists or we need to create it if it doesn't already exist so we're going to create a method here I'm going to say aish all liore CSV this is actually going to be a class method so what I'm going to do is decorate this with the class method decorator so we're going to say at class method what this means is that this will have access to the class itself but it will not have access to an instance of the class if you're familiar with object-oriented programming when you create a new instance of the class we have some different properties on that instance in this case this class method um that we've defined is just having access to the class itself meaning I can access things like other class methods and the class variable that we defined here okay so I'm going to write CLS as the parameter that this will take and what we're going to do to start is we are going to try to read in the CSV file so we're going to say pd. read CSV and we're going to attempt to read the cs. CSV file okay so we're using this variable right here we're going to try to load this in and if that does not work so we're going to say accept on a file not found error then we're going to create the file so we're going to say data frame or DF is equal to PD this is our pandas module remember dot data frame and then this is going to be columns equal to and we're going to specify the four different columns we want to have inside of our CSV file now the First Column is going to be date the second column is going to be amount third column is going to be category and the last column is is going to be description okay let me zoom out a little bit so we can read this a bit easier now if you're a little bit confused here when we create a comma separated uh values file it kind of looks like this we start by having some headings or some columns so in this case date amount category and description then beneath that we have all of the different entries so maybe we have a dates like 207 2024 we have a category or an amount like 12 125 category income description and whatever this is the salary okay this is what the comma separated value file looks like we just have all of the different entries separated by commas pretty straightforward so that's what we're loading in using pandas so that's why I'm specifying The Columns that I'm going to have uh for this what do you call it data frame so that's why I'm specifying these are the four columns that I want to have and I'm putting this inside of what's known as a data frame now a data frame is an object within pandas that allows us to really easily access different rows and columns from something like a CSV file so what we're going to do is we're going to create this data frame and then we're going to actually export the data frame to a CSV file so we're going to say data frame. 2or CSV we're going to say that the name of the file we want is cs. CSV file and we're just going to say index equals false you don't need to worry about that too much right now but it just means we're not going to be sorting this um data frame or doing anything by indexing it okay so what we've done is created a pandas data frame with these four different columns we then have just converted this to a CSV file and what that's going to do is save a local file with this name in the same directory as this python file now what we can do to test this out is we can run this method so I can say CSV Dot and then initialize CSV so let's go ahead and run our code here so let me bring up my terminal and we will go Python 3 and then main.py and we should see that a new file is created with Finance dat. CS s v and it has our four different columns here now just as a sanity check we'll run this one more time and we should see that nothing new happens because the data frame already existed great so now that we've initialized the CSV file and we know that it does exist the next step is we want to actually add some entries to the file so I'm going to make another method here inside of the class I'm going to say at class method and this is going to say addore entry now to add an entry we're going to take in the class we're going to take in some date and amount a category and a description okay these are the four things that we really need when we're adding a new entry into the file and that lines up with our category names okay so for the add entry what we're going to need to do here is kind of use pandas or actually we're going to use what's known as a CSV writer to write into the file so the first thing we have to do is create the entry that we're going to be adding so I'm going to say my new entry is equal to and I'm going to specify all of the different columns associ ass with their values I'm going to say date is associated with the date I'm going to say the amount is associated with the amount I'm going to say the category and this is going to be with the category if we could spell that correctly and then description with the description let's just make sure we spelled this correctly looks good so we're storing this in a python dictionary and that's because we can use the python dictionary to just write into the correct columns uh when we use this CSV writer so you're going to see how this works I'm going to say with open I'm going to open my file which is CLS do CSV file I am then going to open this in append mode so when you put a this means that we're going to be appending to the end of the file we're not overwriting the file or deleting anything or making a new one we're just opening it and then putting the cursor at the very end of the file so we'll add to the end I'm then going to say new line is equal to and I'm just going to specify an empty string right here the reason for that is I don't want to add a new line character at the end when I open this file okay I'm then going to say as CSV file and inside of here we're going to specify our field names so we're going to say field names are equal to and actually it's going to be the same as our columns so what we should really do is we should store this in a variable I'm going to say columns is equal to this and now for the columns we're going to change this to be CLS do columns and then here we can get rid of this variable and what we'll do instead is use the new variable that we just defined so we're going to say writer is equal to CSV Dot and this is going to be the dictionary writer this means we're going to take a dictionary and write that into the CSV file and inside of here we're going to pass the CSV file that we just opened and what else are we going to pass here we're going to pass the field names so we're going to say field names are equal to and this is going to be the cs. column so what we're doing here is creating a CSV writer what this will do is take a dictionary and write it into the CSV file so now that we've created this object all we need to do is use it so I'm going to say WR er. write and this is going to be row and we're going to write a new row using the new entry okay then we're just going to print down here we're going to say entry added successfully like that so let's just quickly recap what we did we created a new dictionary that contains all of the different data that we want to add into the CSV file we then opened the CSV file we opened it in a pend mode and when we use this with syntax here this is what's known as a context manager that's why we have this as CSV file so it's storing the open file in this variable and pretty much what this means is as soon as we're done with the code inside of here this is automatically going to handle closing the file for us and dealing with any memory leaks or just cleaning everything up really nicely so it's great practice when you're working with files to use this with open syntax because you can do anything you need inside of the indented block and then as soon as you're done python will automatically handle closing the file for you which is exactly what's going to happen here so now what I do is I create a new writer using the CSV dictionary writer we pass the CSV file we want to write to and the different field names that we have so it knows how to take the data from our dictionary and kind of associated with the correct column and then we use writer. WR row and we add this new entry there are various other ways that we can write to the CSV file but this is one of the best practice ways to do that okay so now that we've done that let's test using this method so we're going to say CSV do add entry and what we need to pass here is a date amount category and description so let's give a date of maybe 20 07 2024 let's give an amount of maybe 12565 let's give a category of income let's give a description of Sal okay so let's just test if this works by running our code so I'll bring up my terminal and go Python 3 main.py it says entry added successfully and if I go here to my CSV file you can see that the entry has now been added great that is all working and now we have the two kind of main functions we need for our CSV file we have initializing the CSV file and then adding our entry now the next thing I want to do is make it so that from our command line we can ask the user for these various entries and then add it in into the file that way we don't need to just manually write them here we can actually have a program that will continually ask the user if they want to add more entries and they can add it directly from the command line giving us a nice interface to work with the CSV file so let's go over now to our data entry file and the reason I created this new file is so that I have a place where I can write all of the functions related to getting information from the user so that our main file here stays a little bit cleaner and I'm going to show you how we can import those different functions from this file so that again we can kind of organize our code a little bit better so inside of this file I'm going to say from date time import date time and now we're going to write a few different functions now I like to just stub the functions that means just write the um kind of function definition before i' write them all out so we know exactly what we need so the first function we're going to have is get date now when we get the date I'm going to take in a prompt and I'm also going to take in an allow default equal to false now what we're doing here with these different parameters is we're having a specific prompt this prompt is what we're going to ask the user to input before they give us the date the reason why I'm doing this is that we can be getting the date in multiple different places and we may be asking for the date for a different reason so we want to have a different prompt so that this function is a bit more Dynamic now allow default is going to tell us if we should have the default value of today's date the reason why I'm doing this is I want someone to be able to just hit enter and by default it will just select the current date so they don't need to enter the date if the date is today because that's probably a common uh entry method using today's date so if they set this to true then we will allow um kind of the default to be passed okay so that's get date now as well as getting the date we're going to need to get the amount this is the amount of the transaction so I'm going to say pass here next we're going to say Define getor category again we're just going to write pass and then we're going to say get the description like this and we're going to write pass okay so again this is kind of how I like to start so I know the different functions that I need to write and then I can just write them one by one and sorry let me just fix this so it says get description not just description okay so let's start with get date what I'm going to do is say my date uncore string is equal to the input with the prompt so whatever the prompt is was P this function we're going to use that here and then we're going to get some kind of date string now I'm going to say if allow default and not date string then I'm going to Simply return the date time dot today this is going to give me a datetime object in Python but I actually want to convert this to a string so I'm going to say dot St Str and then F time this stands for string format time and I'm going to do percent D percent M and then percent and capital Y now for some reason this is not highlight I think this is okay though yes this looks fine to me and the reason why I'm writing this is that this is actually a format to specify uh the way we want to get our date so in this case we want to have date month and then year so it's going to take today's date which we're getting here and then format it in this format now we're going to return this in the situation where we are allowed the default and the user didn't type anything in so I'm saying okay if they didn't give us any input so if they just hit enter and moved kind of to the next line and we're allowing the default then it's okay we can simply return this otherwise what we want to do is we want to try to validate the date to make sure that what they gave us is valid because we don't want to obviously allow them to enter something that's invalid so I'm going to say try and I'm going to try to create a valid date so I'm going to say valid date is equal to datetime and this is going to be dot St and then P time and same thing we're going to take in now the date string whatever the user entered and we're going to use this format okay so let's paste this right here so what we're saying is okay let's take in the date string let's use this format and let's try to convert this into a datetime object that's valid that's all we're doing now if this line works we can move on to the next line where we're just going to return the valid date and then we're going to copy this line right here and again we're going to format it using this because what we're kind of doing is we're taking whatever they typed in we're converting this into a date timee object at least we're attempting to do that using this format and then since we now have the daytime object we now need to kind of convert that back into the string representation and this will make sure that it cleans up the date that the user typed in and gives it to us in the format that we need now that's only if this works obviously that's why we're putting it in a TR statement it's possible the user can type something like hello and obviously that's not a valid date and well that's going to crash our code so that's why we now are going to have an accept block so if this TR statement does not work work and it would not work because we have a value error then what I'm going to do is just print out the following I'm going to say invalid date format please enter the date in and then I'm going to tell them it should be day day month month year year year format and that's actually the exact format that we're specifying here when we do day month year great then we're going to return and we're just going to call this function again so we're going to say get this and then we're going to call it with the same thing prompt and allow default now this is what's known as a recursive function which means we're just going to keep doing this we're going to keep calling this get date function until eventually we don't hit this case so they do give us a valid date in which case we're just going to return either today's date or whatever the date is that they entered so again what will happen is if they give us an invalid date we're just going to call the function again and then return that result which means now we go back in here if it doesn't work again same thing we go into the recursive case again and we'll just keep going until eventually they give us a valid date now what I'm realizing here is that we have a bit of repetition with the format so I think I'm just going to create a variable I'm going to say format and we'll just call this date format because I believe format is a reserved word I'm going to paste that right here and we'll just replace here the date format in case we want to change that later now we only need to change one variable rather than changing three strings in three places okay so there we go now we're using our date format great now that we have get date let's go to get amount so for get amount we are going to do a try statement and we're going to say the amount is equal to and we're going to try to convert the input into a float because they could give us a decimal value which is a floating point value so we're going to say enter the amount colon and then make sure you have a space just so that there's some room for the user to type we're going to say if the amount is less than or equal to zero because we don't want them to enter a negative amount then I'm going to raise a value error and I'm going to say amount must be a non negative non zero value okay because we don't want them to enter zero because there's no point of having a zero value transaction and we don't want them to give us a negative value because we're only taking positives now if all of that works we can return the amount otherwise we will accept on the value error as e and then we're going to print e and again we're going to return the get amount okay we're going to keep calling this function until they give us a valid amount hopefully that makes sense we're trying to convert this to a float it's possible that this conversion doesn't work in which case we'll immediately go here otherwise if they do give us a valid number we make sure that is non zero and non- negative and then we can return it okay good next we're moving on to get category now we're going to say category is equal to the input and for the prompt here we can say enter the category and we're going to go I for income let's make this capital and and we'll say or E for expense if we want we can quote these with single quotation marks which is what I'm actually going to do here so it's a little bit more clear and notice I can use the single quotation marks in here because I have double quotation marks surrounding the entire prompt if I tried to put a double quotation mark here notice we're going to get an error because it's closing the string off too early okay then we're going to put a CO in and a space so they have some room and we're going to convert this to uppercase so if they type in a lowercase i or a lowercase e this will still work let's zoom out out a bit sorry so we can read this whole line now what I need to do is I need to make sure that they typed in either I or E so actually to do that I'm going to make a variable up here I'm going to call this categories and this is going to be equal to a dictionary I'm going to have an uppercase i and this is going to be mapped to income and I'm going to have an uppercase E and this is going to be mapped to expense okay now we're going to go here to category and we're going to say if the category so what they typed in is in the categories then this is valid so we can simply return and then we're going to return categories at the Key of category so rather than returning I or E we're going to return income or expense and notice we've set this up so now we can add multiple categories later to this if we want to okay and then then otherwise so if that's not the case we're going to have an lse statement and actually we don't need the else statement we can just write it beneath because we have the return here and we can say print like this invalid category and then please enter and then this will be again in single quotation marks I for income and or not and E for expense perfect and then same thing return get the category okay so we doing this recursively until they give us a valid category now the last one is really really easy for getting the Des description we're just going to return and input and since the description is optional we're just going to say enter a description they can type anything they want here because this will be optional okay so now we have our four functions get date get amount get category and get description now we're going to use them from our main file so to do that we're going to go to Main and we need to import them from the file we just wrote so to do that we're going to say from and then the name of our file is data entry so we say from data entry import and then we write the name of our functions so get amount get category get date and get description doesn't matter the order in which you type them perfect now what we're going to do is write a function here that will call these functions in the order that we want in order to collect our data so we're going to say Define add inside of here we're going to begin by initializing the CSV file so we're going to say CSV do initialize CSV and now we're going to get these values so we're going to say the date is equal to get uncore date we're going to say the amount is equal to and this is going to be get the amount for our date by the way just before I forget we're going to say enter the date of the transaction and we'll just put the format we want so day day month month year year year okay and then we'll say um we want something to say that you have the default value so I'm going to say enter the date of the transaction or enter for today's date okay so we'll go with that that's fine and I think we need this here so let's add that and in terms of allow default we're going to say allow default is equal to true so we will allow the default value of today okay now we're going to get the amount that doesn't need any parameters we're going to get the category that does not need any parameters as well and we're going to get the description that does not need any parameters okay once we get these four values we know they're valid because they will have been validated by the data entry functions so now I can simply say CSV Dot and then add entry and now we just pass the value so date amount we got to spell this correctly category and description okay so now we can test this so rather than having these two lines we can just call the ad function let's run our code and let's make sure that this is working okay so enter the date of the transaction or uh or enter for today's date okay Enter the amount let's go with 125 we're going to go with income and description I will go with side gig okay it says it was entered successfully so let's go to finance data and you can see that now we have this entry and it has been added great that is working well okay so let's clear this and now let's move on to the next step which is to kind of make this a little bit prettier in terms of getting all of the input and also to allow us to view a summary of all the different transactions and eventually to actually plot those on the graph so let's go back to this CSV file here and let's add another method so we're going to say add class method because again this is just going to be associated with the class and this is going to be something that can give us all of the trans actions within a date range so we're going to say Define getor transactions and we're going to take in the class a start date and an end date because in order to get the transaction so we need to know the start and we need to know the end now the first thing we're going to do is read in our CSV file so we're going to say dataframe is equal to pd. read CSV and we're going to read the CSV file which with is the CLS dot CSV file name okay now that we have read this in what we're going to do is we're going to convert all of the dates inside of the date column to a datetime object so that we can actually use them to kind of filter by different transactions bear with me this is going to seem a little bit complicated just trust me it will make sense at the end after I can explain it line by line so we're going to say the data frame at date now notice that I can do this when I work with a panda's data frame I have the ability not just to access the individual rows but to access all of the columns which makes it very useful so when I say DF at date I'm actually accessing all of the different values in the date column okay so I'm saying data frame at date and what I'm going to do is change these values so I'm going to say pd. 2or dat time and I'm going to take in all of the values in the date column again and I'm going to specify that the format we want is and then the format that we had which is percent D percent M and percent y now we're going to use this a lot so again we'll put this in a variable it's a little bit redundant that we have the variable in two places but for this purpose or for this project it's fine so I'm just going to put in the CSV file here format is equal to this okay and we'll remove the date format and now we have that in here so we can just use this so I'm going to say CS v. format so we'll use that okay so now we've just converted all of the dates in this date column to that format again the reason we're doing this is just to ensure that they're going to be in the correct object so that we're able to actually sort by them and to kind of find the correct dates because we don't necessarily know what will be stored in this column even though our code is adding it in the correct format this is kind of converting it to a datetime object which gives us some other properties that allow us to um kind of sort by it and use it okay now we're going to say the startor date is equal to date time. STP time okay and we're going to take in the start date and the CSV do format the reason for this is that the start date that's given to us is going to be a string so since it's a string again we want to convert it into the correct format now let's copy this line and just change this with the end date so that we convert these into datetime objects okay so end date and then end date like this and by the way if you're a bit confused don't worry working with dates and times is always very confusing even for experienced programmers all of the stuff you can find in the documentation you can even read here it says new date time pars from a string so what we're doing is just taking a string and then parsing it as a datetime object again so that we can use the properties of a datetime object that allow us to kind of filter by and search for different dates okay now what we're going to do is create something known as a mask now a mask askk is something that we can apply to the different rows inside of our data frame to see if we should select that row or not so bear with me while I write this I'm going to say mask is equal to and we're going to put inside a parenthesis data frame date okay this is going to be greater than or equal to the start date we're going to use the and symbol so not writing and but actually using the and which stands for bitwise and and we're going to say data frame date less than or equal to the end date now what this is doing is checking if the data in the current Row in the column date is greater than the start date and if the data or the date in the current row that we're looking at is less than or equal to the end date the reason why we can do this comparison is because we have all of the values now as datetime objects and we have the start date and the end date as a datetime object so we can actually compare them using the less than and greater than symbols which we couldn't do before if they were simply strings now this symbol you see right here is known as the bitwise and I don't want to explain it a ton but it's pretty similar to the end right here we just use this when we're working with the panda data frame and The Mask specifically so just make sure you have this here and again what this is going to do is just apply this to every single row inside of our data frame and it's going to kind of filter the different elements so let me show you that I'm going to say filtered data frame is equal to DF dolo okay and inside of here we're going to pass the mask what this is going to do is return to us a new data frame a filter data frame that only contains the rows where this was true where the date was greater than or equal to start date and the date was less than or equal to the end date you don't have to understand exactly how this is working this is kind of a fancy feature of pandas but what it's doing again is kind of locating all of the different rows where this mask matches and you could obviously create a more complicated mask but this is what we want for now okay so now that we have that we're going to go down here and we're going to say if the filtered data frame. empty so just checking if there's nothing in it then what I'm going to do is say print no transactions found in the given date range okay otherwise we're going to go into this block right here where we start printing out all of the different transactions and start creating a summary of those so we're going to start with a print statement here here and we're going to say transactions from we're going to make this an F string available in Python 3.7 and above and we are going to say startor dat do string format time and we're going to do the format that we've used before so CSV do format what this is going to do is take our start date and just convert it into this format when we print it out because now we have the datetime object and we're converting it back to a string we're going to say two and then this is going to be the end dat do string format time and then we're going to take in the CSV do format as well okay let's save that so we're just kind of printing this out at the beginning as a header saying hey we're about to show you the transactions in this range and now we're going to print all of those out so we're going to say print and we're going to use our data frame to do this so we're going to say filtered data frame. 2or string this is a method that will just take the data frame so all of the different entries that are inside of it again these are filtered now just the transactions that we want and convert it into a string that we can print we're going to say index is equal to false and we're going to say formatters and now what we can do is specify if we want to format any specific column and we do we want to format the date column so we're going to say date inside of a python dictionary right here so we put the column name as the key and then we put a function that we want to apply to every single element inside of that column if we want to format it differently so the function here is going to be Lambda X and then x. string format time and then we're going to use our format CSV do format okay let me save that now let's just quickly look at this before we go any further so we're saying filter data frame. twring don't worry about the index and then we're using formatters now we pass a python dictionary and if we want to format all of the values inside of the date column then we pass date as the key and we need to pass a function and this function fun will be called with all of the entries from our date column so for example these two right 207 20 24 0707 20 24 whatever the dates are they're going to be passed to this function this is a oneline Anonymous function known as a Lambda function they'll be passed as the parameter X these are going to be datetime objects because we've converted all of the entries in the date column to a datetime object so we're then going to format them so we're going to say string F time with the format so when we print them out we get a nice pretty string okay that's what that's doing now we can go and we can find the total income and the total expense and print those out so we're going to say total income is equal to the filtered data frame and what we're going to do is we're going to look in the filter data frame and we're going to say category is equal to income so this is kind of similar to how we use the mask before we're saying the filter data frame and then what we want to do is we want to get all of rows where the category is equal to income then once I have those rows I want to get all of the values in the amount column and I want to sum those again a feature of a pandas data frame if you haven't worked with pandas before understand this looks a little bit confusing because this doesn't work like a normal list this is a lot different than a list in fact which looks confusing because the syntax is different but we're saying filter data frame again we're getting all of the rows in this data frame where the category is equal to income then from all of those rows we're looking at all of the values in the amount column and then we're summing those so now we're going to do the same thing for the expense so we're going to say total expense is equal to the filter data frame we're going to have the filter data frame category and then equal to expense and then same thing we're going to take the amount and we're going to sum it now we can print this out so we're going to say print I'm going to print a back sln just that we go down one line so that we have a bit of Separation I'm going to say summary and then I'm going to print the total income and total expense okay so I'm going to say total income and this is going to be an F string so that we can do some formatting we're going to put a dollar sign here and then inside of curly braces we're going to say total uncore income now what I want to do is I want to make sure that this gets formatted to two decimal places so I'm going to put a colon and then do 2 F if you've never seen this before what it will do is just round to two decimal places so that we're going to only show at most something like you know 5 67. 89 we won't show that third decimal place we'll just round it off okay so that's what that's doing kind of a useful feature of the FST string next we're going to say print and then we're going to do an FST string again this is going to be the total expense okay we're going to do a dollar sign and then say total expense and then same thing colon. 2f and now we're going to print the net savings so we're going to say print F and then net savings okay now we're going to do a dollar sign and we're going to do a subtraction so we're going to say total uncore income minus total expense now I'm going to put this inside of parenthesis just so that I can format this value and then I'm going to do colon. 2f okay so that's pretty much it and then from here I'm just going to return the filter data frame the reason for that is we then will be able to actually create a plot or a graph using the data frame now that we have it filtered okay so I understand a little bit confusing especially if you haven't worked with pandas before we read this in as a data frame we take the date column and convert it to datetime objects we convert the start date and the end date to datetime objects because they're going to be strings we create a mask which will be used to filter the different rows inside of our data frame we apply that mask using the loc function the lock function we then have a filter data frame if it's empty we just print out that there was no transactions let's add a period here otherwise we print out a transaction summary okay so now we need to actually call this and test it out so let's make a new function call here and we'll say CSV Dot and we're going to go with get transactions now for us to be able to get all of these transactions we just need to put something within this range so let's go with something like like 01 01 2023 is the start date and then we'll just go with a big end date so we can go like 30 07 2024 okay let's see I think that's all we need to pass here so those are our two strings CSV doget transaction okay let's bring this up and let's run our code and you can see that we get all of the different transactions so transactions from these two dates we have date amount category description it prints them out very nicely and then we get a summary total income total expense net savings and it gives us all of those values all right so we're moving along quite nicely now what I'd like to do now is make it so that we have kind of a nice way to input or to work with this program or to work with these different functions where we have kind of a main functions running constantly saying hey do you want to add a new transaction do you want to view transactions what do you want to do and then prompting us for the correct data so rather than having to write these functions here we can just use the command line and interact with our personal finance tracker so to do that I'm going to make a function called Main and we're going to define a while loop so I'm going to say while true and what we're going to do is print out the various options that the user has so we're going to start with just a back sln so there's a little bit of separation and we're going to say one dot add a new transaction okay so that's the first thing that we can do print and then we're going to do two Dot and this is going to say view transactions and a summary within a date right range okay and let's just go and summary then we're going to say print and the third option will just be to exit and then obviously later on if we have more features we can add them here we're going to say choice is equal to input and we're going to say enter your choice and we're going to assume the user is just going to type in a number one through three so we'll give them those options here now we're going to say if the choice is equal to one then they want to add a new transaction so if they want to add a new transaction we can just call the add function that we wrote here so they can add a new transaction okay we're going to say l if the choice is equal to two then what we need to do is we need to get a start date we also need to get an end date uh and then we need to get the transactions so we're going to say startor date is equal to get date now for this we're going to say enter the start date we're not going to allow them to enter the default value so we'll just leave that false but we'll give them the format that we want them to enter it in okay now let's do the same thing for the end date this is equal to get the date and let's copy this prompt and just change this to say the end date okay so here and then this will be the end date great so now we have the start date and the end date now we need to pass it to our file so we're going to say CSV Dot and this is get transactions and we're going to pass the start date and the the end dates okay now we're also just going to say data frame is equal to this because we're going to use the data frame later if we want to plot this on a graph and then otherwise we're going to say l if Choice equals 3 well that means they're going to exit so we're going to say print exiting dot dot dot and then we're going to say break so we're going to break out of the wall Loop and then lastly else we are going to say print we're going to say invalid Choice enter 1 2 or three okay period and that should be it now we just need to call this function so we're going to say if underscore name equals equals uncore uncore maincore uncore then we're going to call Main now the way that this works is that if we run this file directly this line will execute and we'll call the main function whereas if we import this file this will not run because the name is not going to be equal to main pretty much what this line is doing is protecting this code so that we don't run the main function unless we directly execute this file if we were to remove this and just call the main function even if we were to import something from this file then this main function would run which we don't want so just kind of a good practice and that's why I'm putting it here so let's quickly test this out and make sure that this is working and then we can move on to the graph part which is pretty cool so let's run our code and you can see it says add new transaction view transactions exit enter our choice let's go one enter the data of the transaction action so let's go 20 or let's go 19 um maybe 07 2024 the amount let's go with 3,000 let's make this income let's just call this salary and then it says entry added successfully now maybe we want to view the transactions let's go with 0707 2024 uh let's go with 30 07 2024 okay you can see that it gives us our different transactions gives net income net expense and then our net savings and that is very good okay now let's try to exit and you can see that we can exit the program obviously we could make this look a little bit nicer but for now I think this is quite good and now we want to move on to actually creating the graph so the idea is after the user gets a list of different transactions we want to ask them uh if they want to plot it and they want to see it on a graph well in order for them to see it on a graph we need to make that graph and to do that we're going to use map plot lip so we're going to make a new function here and this is going to say plot uncore transactions this is going to take in a data frame the data frame is going to contain all of the transactions that we want to plot and we need to do a little bit of configuration here in order for this to work first things first we're going to say DF do setor index and we're going to make this the date now the index is the way in which we locate and manipulate different different rows so we're using the date column because that's how we want to kind of find different information we're also going to want to kind of sort by the date and find the correct information based on the date to create the plot I know seems a bit weird but we're going to say in place equals true which means we are going to modify the data frame in place when we set the index as the date again this allows us to kind of find different rows and entries using the date column and to have more efficient operations when we're working with the date okay now that we've done that what we need to do is create two separate data frames the income data frame and the expense data frame the reason for that is that I want to have income as one line and expenses as another line so I need to just have all my income and all my expenses in two separate kind of data sets or two separate data frames in this case so I'm going to say my income dataframe is equal to and we're going to start with the data frame and like before we're going to say data frame category is equal to income now this is good and right now this is going to give us the uh all of the sorry rows that are income so that's good however we need to do a few different things here in order to kind of shift this data frame into something that's going to be nice to actually plot on the graph the idea is I want to see a nice line where it's showing me even potentially zero income on different days so I might need to actually add more rows in where we have uh kind of missing dates I know this seems weird but if we look at this you can see that we have something on 20 7th and 19th the rest of the days we don't have anything so if we were to plot this we're just going to get three kind of dots or three disconnected lines what I want instead is I want to have all of the empty days be marked as zero so that we actually see the line going in the correct direction you'll see what I mean when we plot this out but it requires a bit of manipulation to this data so the first thing we're going to do is we're going to apply a resample to this and we're going to pass the value D so this stands for daily frequency so we're going to take our filter data frame with all of the transactions that we want we're going to look at them and we're going to make sure that we now have a row for every single day that's what resampling is doing it also allows us to aggregate different values that are on the same day I know a little bit confusing these methods are not super simple to understand but think of it as just kind of filling in all of the missing days that's really what resampling is doing now next what we're going to do is we're going to sum now what we're doing here is we're summing all of the different amounts so that's what sum is going to do is just going to take the values and add them together and now we're going to reindex and this is going to be with the data frame. index and we're going to say the fillore value is equal to zero now what this is doing here is making sure that the index is correct after we've applied these different operations so we've resampled which means we're pretty much filling in all of those additional rows and having our data be stored in Daily frequency we're summing our values when we sum our values we're going to aggregate the rows that have the same date and add their amounts together and then we reindex and when we reindex we just make sure that the data frame is going to conform to this index and we fill in any missing values with zero again I know a little bit confusing just trust me this works okay so let's save this and let's go with the next line here which is going to be the expense data frame which is going to be the exact same thing except the category is simply expense so now we have a data frame for our income and for our expenses and the next thing we need to do is create a plot using map plot lib where we can actually uh put this different data so we're going to go to the top of our program here and we're going to say from actually not from we're going to say import matplot li. pyplot as PLT okay very uh common import here now what we'll do is we'll start creating a plot so we're going to say PLT do figure figure is uh kind of setting up the screen or the canvas where we're going to actually be putting the graph and we can specify the size that we want so we're going to say the figure size is equal to 10 and 5 okay you can change the size later if you want and now we can plot the different lines or in this case the different data frames that we have so we're going to say PLT do plot and we're going to plot the income data frame and we're going to plot the index the index means we're plotting the date pretty much then we're going to say the income undor data frame and the amount so what we're using as the x axis is the index so all of the different dates what we're using for the y- AIS or the value is the amount then we can give this a label and we're going to say that this is income and we can specify the color and if we go with G that just stands for green now we can do the same thing for our expense so we're going to say this is the expense data frame. index which again is the we then are going to say this is the expense data frame and the amount we will give this a label which is equal to expense and we'll give this a color which can be r or red okay now we can just do a few labels so we're going to say PLT dox label and this is going to be the date then we can give this a y label so we're going to say PLT doy label and this is going to be the amount we then will give this a title so PLT do title and we can say income and expenses over time feel free to change this if you want we'll say PLT do Legend when we do this it enables The Legend So we are going to see the labels for our different uh colored lines and then we're going to say pl. grid this enables the grid lines so we're going to specify that as true so we can more easily see where our values line up and then lastly PLT do show this actually takes the plot and shows it on the screen so plot transaction should now work what I'm going to do lastly is I'm going to go here to where I have my choice and I'm just going to say if input do you want to see a plot question mark and we'll say Y no and this is going to be dot lower is equal to Y then we will simply call that function which is plot transactions and we will pass that filter data frame okay so I'm just asking them hey do you want to see this converting it to lowercase if they type in yes or why then we will plot this and it should show the graph okay so let's test this out but actually before we test this out I'm just going to fill this with some sample data so that it looks a little bit more full so I just added a bunch more entries here into my CSV file I actually just used chat GPT to generate a bunch of them for me so it was really really fast and you can see that we're going from kind of the 1st to the 31st of uh what is this July so those are kind of the dates that we can choose from all right so let's run our code now so let's go up here and run and let's view our transactions uh let's start at the 1 of January so 01 or not January sorry of July 2024 let's go maybe up into the middle so 157 2024 and you can see that it gives us a bunch of the different transactions we have total income 3,000 total expenses 9005 net savings 2145 do you want to see a plot yes of course we want to see a plot and then it brings us here and you can see that we get the plot now notice that we have a bunch of days where we have zero the reason again why we have that is because of the resampling that we did to the data frame so that we have this on a daily frequency so whatever the dates are this is giving us everything daily even though it doesn't quite look like it with the legend because it's skipping a few dates it is indeed doing that so there you go that is how this works we can test this out again with a different date range just to make sure so let's go to uh let's go maybe 03 uh 07 2 24 and let's go 30 07 2024 do you want to see a plot yes and when we come here you can see that we get a different plot now that's going from 03 to 030 there you go guys that is going to wrap up this video so I know that a lot of this was a little bit confusing because you're learning about some new modules like map plot lib and pandas but I wanted to show you some more advanced features in Python and how you create a real usable application obviously there's a lot of more features that you would want to add this for it to really become super useful but this is a really solid base that you can extend and hopefully will act as a really good starter project and something you can turn into maybe even a resume project all of this code will be available from the link in the description if you need it if you enjoyed the video make sure you leave a like subscribe to the channel and I will see you in the next one [Music]
Info
Channel: Tech With Tim
Views: 17,864
Rating: undefined out of 5
Keywords: tech with tim, learn python with 1 project, learn python through projects, python tutorial project, how to do python project, python project tech with tim, learn python, python how to, tech with tim python, python, python programming, coding, python mastery, master python
Id: Dn1EjhcQk64
Channel Id: undefined
Length: 56min 54sec (3414 seconds)
Published: Fri Jul 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.