Importing and exporting CSV files in Python
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.
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.
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.
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.
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).
All the code can be found on my GitHub: https://github.com/kasiarachuta/Blog/blob/master/Importing%20files%20into%20pandas.ipynb