Pandas Write To CSV β pd.DataFrame.to\_csv()
Save DataFrame as CSV in Pandas
Ah, your work is finally done. Youβve made your models and gathered your data insights. All that is left is to save your work. But oh no! How do you do this? That is where Pandas To CSV comes into play.
Pandas To CSV will save your DataFrame to your computer as a comma separated value (CSV) datatype. This means that you can access your data at a later time when you are ready to come back to it.
I save my data files when Iβm at a good check point to stop. This means that Iβve done my transformations, and Iβm ready to have a record of new data. Or use the data elsewhere β Like uploading to Google Sheets
Pseudo Code: Write your Pandas DataFrame to a Comma Separated Value file (CSV File)
Pandas To CSV
Pandas .to_csv() Parameters
At a bare minimum you should provide the name of the file you want to create. After that I recommend setting Index=false to clean up your data.
- path_or_buf = The name of the new file that you want to create with your data. If you donβt specify a path, then Pandas will return a string to you.
- index = By default, when your data is saved, Pandas will include your index. This can be very annoying because when you load up your data again, your index will be there as a new column. I highly recommend setting index=false unless you have a specific reason not to.
- sep = By default your file will be a βCSVβ which stands for comma separated values. This literally means that your data is separated by commas. However if you didnβt like commas, you could set the βsepβ to something else. This separator is usually referred to as the βdelimiter.β
- columns = Columns to write. If you only wanted to save a subset of your columns, you can specify that subset here.
- header = Say you wanted to switch your column names, then you can specify what you want your columns to be called here. This should be a list of the same length as the number of columns in your data.
- Other Parameters β Other parameters are not often used and wonβt mentioned here. If youβre curious head over to the official documentation (below) and check them out.
Now the fun part, letβs take a look at a code sample