From the course: Advanced Hands-On Python: Working with Excel and Spreadsheet Data

Unlock the full course today

Join today to access over 24,600 courses taught by industry experts.

Writing CSV and Excel data

Writing CSV and Excel data

- [Instructor] Of course, we don't want to just read data with pandas, we want to be able to write data as well. So for this example, we're going to open up our pandas_write.py file. And you can see that I've defined an array of objects that I would like to write out to a CSV file. And you can see that the data has the same format as our inventory.CSV sample file. It's just a lot smaller and it's in the format of dictionaries rather than an array of lists. So what I need to do first is create a data frame to represent the data. So to do that I'm going to create a data frame variable, and on pandas, I'm simply going to use the data frame constructor and pass in my array of objects. Then I need to save the data as CSV format, and I can do that by using the appropriately named to CSV file. So I'll call DF and I'll call to CSV and I'm going to name it output.CSV. All right, so let's go ahead and run that. And when I do, you can see that output.CSV gets created and we can go ahead and look…

Contents