How to save Python code as a file (CSV, TSON, etc)

Akansha Yadav shows how to save a pandas DataFrame to CSV, Excel, JSON, and HTML files. She builds a synthetic employee dataset with NumPy, then uses .tocsv, .toexcel, and .to_json with parameters like index control and JSON orientation. The code is on GitHub.
Chapters
Ladies and gentlemen, hello everyone, welcome back to my YouTube channel. In today's video, we will be talking all about pandas. Yes, it is day 4 of pandas. So let's see what are we going to learn in this video. On my screen today, what we are going to do is we have created multiple data using series, using DataFrame. Now I am going to tell you how you guys can save that data in any format. Format in sense like, if I wanted to save that data as an Excel file, then how I can do that? If I wanted to save it as an CSV, then how I can do that? So to make this happen, what I have done is I have already written the code. Now I am going to give you the explanation of the same code. If you want the file of this code, you can check the description for the link of the GitHub repo where I have already uploaded this. So let's get back on the screen. And so first what I have done is I have imported the pandas library and then I have imported the NumPy library. So till now we have seen that how to create data using series and using the DataFrame. Now I am going to Use the NumPy for generating the random data. So here I have generated a random data. I have a variable data. Now I have name column, age, salary, department, year of experience, education level, performance rating, hire date, location, and marital status. Everything I have generated randomly. So let's see how. So first is I have written name employee plus i for in, uh, like I have written, uh, the output will be employee 101, employee 102, something like that. So over here I am using string i for i in range 1 to 101. So it means it will start from employee 1, employee 2, and will go up to 100. In the age, what I have done is I'm using numpy.random.randint and giving it a range 20 to 60, and this 100 is step. Same way I have done for salary, random.randint, it means a randomly integer I am generating from 3 30,000 to 1 lakh, and the step count is 100. For department, I have, I have entered 4 departments: IT, finance, marketing, and HR. Out of these, I am just making a random choice. And year of experience, same, I'm using random.randint. And then education level, I am writing it as these are 4, 4 are the values which I have written, and random.choice is helping me to get the education level. Same way, I have generated the entire data, which I'm going to show you how you can save it locally. So suppose you wanted to save it in the data CSV format. So for, for this, what you have to do is you have to create a DataFrame. So we have the data, now we have to create the DataFrame to CSV. I have just written df_csv. Now I am creating a DataFrame and entered a data in the bracket. To save it, you have to use .to_csv method. Then you have to write down your file name, what you wanted to give this name. And then you have to write index is equal to true or false, you want the indexes or not. If you want, then you can make it true, else you can keep it false. If you wanted to save it in the Excel format, then you will write .to_excel, then the file name, then the same way Excel indexes or not. Another way is the JSON file. If you wanted to generate a JSON file using the data that you have, then it will generate the JSON file using the command .to_json, and data.json will generate an file for you. orient is equal to records. Same way you can do for HTML. So these 4 are the data formats which you will be working a lot. Like if you wanted to work on the web pages and you are extracting the data, then you will find an HTML format there. If you're working locally or downloaded a file or like that, then you have to work on the CSV or the Excel file. Apart from that, you might be using the JSON files as well. So these four are the ways using which you can save your data locally. Now I'm going to run this file for you to just show you the output. It, it get connected to the kernel. Now we have— so we are first importing data. So here we have the data CSV file, it says. Okay, so here we can see that the files are generated, whichever name I have given, the files are generated for the same. This is HTML file, this is JSON file, this is CSV file, this is Excel file, which I can't view in this Visual Studio Code, but yes, it is generated. So that's how you can save your data locally. If you like this video, don't forget to hit the subscribe button, comment down your views. I'm waiting for you all in the comment section. Bye-bye!










