 Welcome back. I'm Dan. And I'm Nathan. And we're here today to talk about reading and writing PANDA's data frames to and from files on your file system. Let's go throw some data in some file cabinets. So if you were starting here with a data frame object called DF, we made that in a previous video. So if you haven't seen that video, go check it out and I'll show you how we instantiated that PANDA's data frame object. So here we'll just take that data frame object just to refresh our memories as to what's in it, use the head method and you can see that it's a list of cities with prices of iPhones in that city. So what we're going to be doing here then is taking that data frame and storing it in a file. There might be situations where you need to either pass that that object to a different program or you need to need to persist so that you can load it again at a later time into Python. And that would be a couple of reasons why you'd want to want to output to a file. Exactly. Like sometimes if you had to shut down the whole process and started back up another time, you don't necessarily want to have to ping Craigslist 8000 more times since our list right here is at least 8000 rows long. It saves a lot of time if we save it as a file and just load the file back in. It's a good point. So let's do that. So it's PANDA's provides a number of methods for exporting to a bunch of different file types. You can just take a look at the excellent documentation at pandas.pydata.org. You can see it can read from CSV, Excel, JSON, HTML, you name it. It can also send data to those formats as well. So be sure to check that out. It's great documentation. We're just going to pick a couple of them and time it just to see how fast each of them work. Yeah. So one of the really neat things about these IPython notebooks is the magic commands. You'll hear that referred to the magic commands. We're going to be doing a series of videos at some point on those magic commands because there's a lot of them and they're really cool. The one we're going to mention here is the time it magic command. What this does is it just runs through a bunch of loops and shows you the best time that it took to run that one single command. So yeah, we'll do just to show you how long it takes to write to each of these file types. We'll take the data frame DF and go to CSV and let's just call it iphonedata.csv and we'll hit enter and all you need is just a file name and you can see that took 22.9 milliseconds per loop. Let's try. How about an Excel file? Yeah, that was one of the options. Excel is of course a pretty common file type too. So we'll call that iphone underscore data dot Excel and you can also use these if you wanted to go read this data in another program. Excel, for example. Wow, quite a bit longer. Almost 30 times slower. Yeah. So how about another common type HTML? We can even say this straight to HTML format. And yeah, once it's in HTML format, you could even open it up in a web browser if you wanted to. It'll be a nice little table. See how that does. It's feeling slow. And a little bit slower than Excel. Wow there. One less common data type, which is very compressed in high performance. So this would be very useful for, say, not necessarily 8,000 rows, but say 8 million rows is the hdf5 format. And we got to add our two underscores in front of the hdf2. Oh, you're always catching my typos. Of course, it's good to have you around pair programming, right? Yeah. Yeah, that's one other thing that this needs is not just a file name, but it needs a table name or database name. So if we just run this, this also gives us one more error. But before we get to there, notice that it only took us 11 milliseconds per loop. That was half to a third the length that the CSV file took even. So quite significantly faster. This error up here is just saying that it's not as fast as it possibly could be because we're not using C type variables. C types would be all your ins and characters and float values. We're instead using strings for both the city and just to see here, actually. So now that you have these files on your file system, we're going to go ahead and just load them into a new instance of Python. So I'm going to start just a new Jupyter notebook here. I'm going to need to import pandas again, since that's what we want to import it as. Yeah, so we'll do PD make it a data frame again, we'll call it df equals PD dot read. And in this case, we'll just read the CSV. And I think I call that iPhone data dot CSV. The thing about reading in CSVs is that you need to sometimes you might want to specify which is the index column. And so in this case, I'll just do an index call equals zero. So the first the first column that pandas encounters, when it's reading this file, it's going to make that the index. And let's just take a little head here, take a peek at what that whole thing that works. And sure enough, we have it again. So next time we'll read it back in this data, find some latitude and longitude for all the cities and add a couple more rows, I mean columns to this table. Thanks for joining us and we'll see you next time. Be sure to hit subscribe.