Day-12 Project based on pandas & numpy Library | implement what you learnt | Codingdidi

Akansha Yadav works through six interview-style pandas and NumPy questions on a synthetic stock price and sales dataset. She covers computing averages over time periods, finding peak months, calculating percentage changes, and comparing companies using filtering, grouping, and rolling averages.
Chapters
Ladies and gentlemen, hello everyone, welcome back to my YouTube channel. This is another video in our series, Panda Series, which is, which is going from scratch. And now today we are going to implement what we have learned so far in this series into one project, and that project is also, oh, we are relating it to the interview. So let's get back on the screen and see what the data is and how we created the data and what are the questions that you might be asked in the interview and how you have to deal with it. So what I'm doing in this is I'm generating, uh, data using pandas and NumPy library. So here I have imported both the libraries. Then after importing the libraries, I have the date range variable in which I am generating the date data. So this is how our data should look like. One is the date column, another one is the company X stock price, then we have the company Y sales, and then company Z stock price. So these are the 4 columns that I am creating. And along with that, we should have the 1000 rows. So that is why I am taking a period of 1000 over here, and the frequency is day because I want the dates to switch by day only. So here is my data in which I have the date which is going to be the same one which we have already defined above. After that we have the stock price, sales, and stock price. For this we are using random.randint because these are going to be this random integers. And I am taking the low, high, and the size— low, high, size— low scale and size. That depends upon you how you wanted to generate the data. So then I am printing it using .head. So this is how our data looks like. So these are the questions that you might be asked in the interview, and I am relating what Whatever we have learned throughout the series, we will be implementing everything on this data. So what I have done is I have first, I have checked whether we are getting the correct number of rows and the columns or not. So yes, we are getting, we are getting 1000 number of rows and the 4 number of column. After that, I have saved my data into the company_data.csv file where I have made indexes to the false because I don't want indexes to be there. And then I have printed a statement saying that data saved to this, this file. So this is how my data file looks like where I have these data. So let me go back. Okay, so this is our question number 1. The question number 1 says that you have to calculate the average stock price of Company X over the last 6 months. Over the last 6 months, it means the day— the duration is already mentioned in the question. So for that, what you have to do is you have to follow one— you have to follow certain steps. So the number 1 step is that you have to set the today's date, means you have to take a variable, then you have to set the today's date so that you can go back to the duration that is mentioned in the question. After that, you have to filter the data for particular duration that is mentioned in the question again. Calculating the average stock price. So let's see how to do it. So first I have created a today variable in which I have added the today's date. After that I have to go back like 6 months ago, what was the prices and all. So I am decreasing it, it means I am subtracting the actual one which is today minus timedelta using— and in a 1 year we have 365 days, so I'm dividing it by 2 which will give me the days, number of days in the 6 months. And I, I'm just going back using the subtract one. And here we are with the data in the variable last 6 months. And then I am applying mean function to get the average of that particular duration, and then I am printing it using the print statement. This is how you have to implement it, whatever, uh, the things you have learned, and that should be in the pattern because as per the questions you have to go, it is today's date, so you have to use the today's date. We are using today's date, and it— in the question, we— they have already mentioned about the last 6 months. It means you have to go back last 6 months. So in the pandas, there is one function called timedelta, and you can use that function for this. Let's see another question, which is identifying the months with the highest total sales for the company while using their monthly sales data. So they are asking about the monthly sales data. It means first what you have to do is you have to extract the month, then you have to calculate the monthly sales, and then you have to find the highest sales months, and then you have to print the results. So again, what we are going to do is we are going to create one variable in which this time it's gonna be the column name. So I want one column which is month, and I am going to extract it from the date. And date is in the place of indexes. If you have observed over here that we have like, we have the date after the indexes. So we are going to use this particular data, date, and extracting the month. After that, I am going to apply two things because now we have two columns. One is the month, another one is the company Y sales data. And I am going to apply .SUM method on this, and this will give me the monthly sales data of company Y in this variable. Then I am going to apply two functions. First is I wanted to know in which month there was a highest sales, and second is I wanted to know by what amount it was. So first is I am using IDXMAX, which is going to give me the maximum sales month, and another one is the max. And both the things I'm printing. So here we got the result, which is April, and we are getting the total sales of 40— 45 lakhs and 30,300. And let's get back on to the next question, which is our question number 3. We have to find out the minimum and the maximum stock price of the company Z on the given day in the last year. So for this, you have to follow 3 steps. First is you have to filter the data. It means it's talking about the last year. So you have to filter the data. Second is you have to do the reshaping. Third is you have to look for the daily extremes. So first, to filter out, what we are going to do is we are going to use the today's date for the reference. Then I am going to apply the TIMEDELTA function. It's talking about the last year, it means -365 days, and that is what I am doing over here. You do not have to do the direct subtraction because you have to use the TIMEDELTA. It will take the dates and then only it will subtract it. And then I am going to reset the index. It means I am going to reshape to get the daily data, assuming the date is an index. So that we can easily get it. And I am applying the max function and the min function to get it. So here we have the company stocks price, which of company Z, and same way I'm applying max and min function on the, the same data. And here I am printing it. So here we got the output that this, the maximum one is the 140 and the minimum is 42. Another question that we have in this list is that you have to create a column in the DataFrame showing the percentage change in the stock price for the previous day for the Company X. So what you have to do is you have to use the PCTCHANGE function in this because we are talking about the percentage change. So you have to use the PCTCHANGE multiplied by 100. I am creating a new variable, it means the new column I am creating over here, which is Company X daily change, so that I can get to know about the daily changes and I can showcase it in the form of the percentage. Then what I am trying to do is I am going to handle the first day's missing value. So there is one first column which might have the missing value if you are going to change something. So for that, what I am trying to do is I am trying to set it to the 0 so that I will have the consistent data throughout. So, and then I am printing it using the head. Now let's see the next question, which is you have to find the number of days when the stock price of the company Y was above its 30-day moving average. So very first thing that you have to calculate is the 30-day moving average that we used to, you know, track when we are, we are into the stock market. So you have to first calculate the 30-day moving average. For this, you might have to use the functions. One is the ROLLING function, another one is the WINDOW function. This will give you the correct data about to get the 30 days moving on. And apart from that, you have to do the second thing, which is you have to calculate above. It means which values are above that mean value. So let's see how to do it. First is you have to create one variable, it means one column, that is Company Y and 30 days moving average. Then you have to take the data of that particular company, which is Company Y sales data. Then I'm applying the dot rolling and then window 30 I am taking because 30 days moving average again, then calculating the mean. So in this variable we will have the mean. Now what I have to do is I have to check the days above moving average. So for that what I have to do is I have to use the comparison operators, and this is my comparison operator. I am checking it and then I'm subtracting it by 29 because 20— subtracting 29 for the initial missing, and then I am printing the results. So there are two things that we have followed. First is you have to calculate the 30-day Moving Average, then you have to calculate the days above MA. Last but not the least question, which is question number 6. You have to compare the average stock price of Company X and Z in the first quarter of the year. First quarter in the year. So earlier in the previous questions, we were asked about the 6 months, then monthly data, and then the last year and all. But this is the first quarter. So, so the year is— throughout the year we have the 4 quarters, the 3, 3, 3 quarters. So what you have to do is for this, you have to follow these steps. First is you have to filter by quarters, and then you to calculate them. So first, what I have done is I have taken a variable in which I have stored the today's date. Now I am going back to the first quarter. So for this, what you have— what you can do is you can use .quarter function, and I'm using it equal to equal to 1 because we are talking about the first quarter. So if you are talking about the second one, you can replace 1 with a 2. Then you have to calculate the average stock price of both the companies. So these are the mean value— mean functions that you need to apply for. And then you have to print the things. Additionally, what you can do is you can use the conditional statements to provide more informative results. So in this case, what we got is we got Company X as the higher average price in the quarter 1. So this video was all about the interview questions that might be put up based on the pandas. So we are going to come up with more— means I'm going to come up with more practical and scenario-based things in this series. So don't forget to subscribe my channel if you want. You can hit the like button and comment down your views. We are too— I'm totally open to the views that you are going to put on in this comment section. I would be happy to serve you with the things that you you might request in the comments. So we'll meet in the next video. Until the next video, bye-bye.










