How can I create a CSV file?

Question

How can I create a CSV file?

Answer

There are several ways you can create a CSV file which you can use to store data which can then be utilized by modules such as Pandas.

Text Editor

  • You can use any text editor like Notepad to create a CSV file, by saving the file with the extension .csv.

Microsoft Excel

  • Using Microsoft Excel, you can save a file as a CSV file type. To do so, choose “Save as”, and under the “Save as type” option, choose “CSV (comma delimited)”.

Google Docs

  • If you have a Google account, you can quickly create a CSV file. To do so, open a Google Spreadsheet, then choose:
    “File -> Download as -> Comma-separated values (.csv, current sheet)”

If you know of any other ways to create a CSV file, feel free to share them here!

1 Like

CSV is a simple file format used to store tabular data, such as a spreadsheet or database. Files in the CSV format can be imported to and exported from programs that store data in tables, such as Microsoft Excel or OpenOffice Calc.

4 Likes

It seems Pandas has .to_csv() method to write a DataFrame to a CSV file.

import pandas as pd

df = pd.DataFrame([
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],
        index=['row1', 'row2', 'row3'],
        columns=['col1', 'col2', 'col3'])

df.to_csv('some.csv')
7 Likes