Do you need to import a CSV file into Python?
If so, in this post, I’ll show you the steps to import a CSV file into Python using pandas.
To start, here is the general syntax that you may use to import a CSV file into Python:
import pandas as pd from pandas import DataFrame ReadCsv = pd.read_csv (r'Path where the CSV file is stored\File name.csv') df = DataFrame(ReadCsv,columns=['Column name 1','Column name 2','Column name 3']) print (df)
Next, I’ll review a simple example with the steps needed to import your file.
Importing Data into Python
So let’s begin with a simple example, where you have the following client list and some additional sales information stored in a CSV file:
|Client Name||Country||Product||Purchase Price|
This is how the info would look like in the file (where the CSV file name is ‘Client_01-FEB_2018’):
Steps to import a CSV file into Python using pandas
(1) Firstly, capture the full path where your CSV file is stored. In my case, the CSV file is stored under the following path:
You’ll need to modify the Python code below to the path where your CSV file is stored on your machine. Don’t forget to include the:
- File name (as highlighted in green). You may choose a different file name, but make sure that the file name specified in the code matches with the actual file name
- File extension (as highlighted in blue). The file extension should always be ‘.csv’ when importing CSV files
(2) Type/copy the following code into Python, while making the necessary changes to your path.
Make sure that the number of columns and names in the CSV file match with the number of columns and names assigned in the Python code.
Here is the code for our example (you can find additional comments within the code itself to clarify some of the components of the code):
import pandas as pd from pandas import DataFrame read_clients = pd.read_csv (r'C:\Users\Doron E\Desktop\Import_pandas\Client_01-FEB_2018.csv') #read the csv file (put 'r' before the path string to address any special characters, such as '\'). Don't forget to put the file name at the end of the path + ".csv" df = DataFrame(read_clients,columns=['Client Name','Country','Product','Purchase Price']) # assign column names print (df)
(3) Finally, press F5 to run the Python code. This is the result that you’ll get:
Did you know that you could also import the CSV file into Python without specifying the path?
In the final section of this post, I’m going to share the code to create a simple GUI to import the CSV file into Python.
Create a GUI to import the CSV file into Python
You may use the code below to import a CSV file into Python.
This code uses the tkinter module. Once you run the code, you’ll see a small display with a single button to import the CSV file.
import tkinter as tk from tkinter import filedialog import pandas as pd from pandas import DataFrame root= tk.Tk() canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised') canvas1.pack() def getCSV (): global df import_file_path = filedialog.askopenfilename() read_file = pd.read_csv (import_file_path) df = DataFrame(read_file) print (df) browseButton_CSV = tk.Button(text=" Import CSV File ", command=getCSV, bg='green', fg='white', font=('helvetica', 12, 'bold')) canvas1.create_window(150, 150, window=browseButton_CSV) root.mainloop()
Press F5 to run the code in Python, and you’ll get this display:
Simply press the button and then choose the location where your CSV file is stored.
We just saw how to import a CSV file into Python using pandas. At times, you may need to import Excel files into Python. If that’s the case, you can check the following tutorial that explains how to import an Excel file into Python.
To find out more about using pandas in order to import a CSV file, please visit the pandas.read_csv documentation.