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 df = pd.read_csv (r'Path where the CSV file is stored\File name.csv') 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 CSV file (where the file name is ‘Clients’):
Steps to import a CSV file into Python using pandas
Step 1: Capture the file path
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 reflect the path where the CSV file is stored on your computer. 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
Step 2: Apply the Python code
Type/copy the following code into Python, while making the necessary changes to your path.
Here is the code for our example (you can find additional comments within the code itself):
import pandas as pd df = pd.read_csv (r'C:\Users\Ron\Desktop\Clients.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" print (df)
Step 3: Run the code
Finally, run the Python code and you’ll get:
Optional Step: Selecting subset of columns
Now what if you want to select a subset of columns from the CSV file?
For example, what if you want to select only the Client Name and Country columns. If that’s the case, you can specify those columns names as below:
import pandas as pd data = pd.read_csv (r'C:\Users\Ron\Desktop\Clients.csv') df = pd.DataFrame(data, columns= ['Client Name','Country']) print (df)
You’ll need to make sure that the column names specified in the code exactly match with the column names within the CSV file. Otherwise, you’ll get NaN values.
Once you’re ready, run the code (after adjusting the file path), and you would get only the Client Name and Country columns:
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 a CSV file into Python.
Create a GUI to import a 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 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() df = pd.read_csv (import_file_path) 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()
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.
You 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.