Importing and exporting CSV files in Python

Kasia Rachuta
2 min readMay 27, 2016

--

When doing data science in Python, you may be asked to analyse the data that’s in CSV or Excel file. You need to be able to read this file into Python. Using pandas will help you to automatically convert it into a DataFrame.

Today, I am going to show you how to both import and export CSV files. If you have looked at any of my previous blog posts on my GitHub account, you saw that I already regularly import files into my IPython notebooks.

Reading CSV files

object_name = pd.read_csv(“file_name.csv”)

Ensure to import pandas as pd before running the above method.

I have just imported a CSV file and viewed the first 5 rows

dframe = pd.read_table(‘file_name.csv’, sep=’delimiter’)

You can also specify the delimiter by passing the argument of ‘sep’— usually it’s a comma but you can sometimes come across different delimeters.

I specified the delimiter as a comma

dframe = pd.read_csv(“file_name.csv”, header=None)

Using the argument header=None when reading the CSV file won’t use the first row as the column names.

Not using the first row as the column names
In my file, the first row contains the column names so it looks odd

dframe = pd.read_csv(‘file_name.csv’,nrows=number)

When reading a CSV file, you can specify the number of rows to be read. In my example, I choose only the first 10 rows.

This DataFrame continues for the 10 rows I requested — you can view that on GitHub

Exporting the DataFrame as a CSV file

dframe.to_csv(“file_name.csv”)

The dataframe is exported as a CSV (note: it will be in the same file as the IPython notebook you’re working on).

dframe.to_csv(“file_name.csv”, sep=’delimiter’)

You can specify the delimiter — for example, you can use question marks instead of commas (which are the default delimiter).

Specify the delimiter as a question mark

All the code can be found on my GitHub: https://github.com/kasiarachuta/Blog/blob/master/Importing%20files%20into%20pandas.ipynb

--

--