Need to import a CSV file into Python?
If so, you’ll see the complete steps to import a CSV file into Python using Pandas.
To start, here is a simple template 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, you’ll see an example with the steps needed to import your file.
Importing the 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 (where the file name is ‘Clients‘):
|Person Name||Country||Product||Purchase Price|
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.
For example, let’s suppose that a 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 in the path, 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:
Person Name Country Product Purchase Price 0 Jon Japan Computer $800 1 Bill US Tablet $450 2 Maria Canada Printer $150 3 Rita Brazil Laptop $1,200 4 Jack UK Monitor $300 5 Ron Spain Laptop $1,200 6 Jeff China Laptop $1,200 7 Carrie Italy Computer $800 8 Marry Peru Computer $800 9 Ben Russia Printer $150
Optional Step: Select 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 Person Name and Country columns. If that’s the case, you can specify those columns names as captured below:
import pandas as pd data = pd.read_csv (r'C:\Users\Ron\Desktop\Clients.csv') df = pd.DataFrame(data, columns= ['Person 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 Person Name and Country columns:
Person Name Country 0 Jon Japan 1 Bill US 2 Maria Canada 3 Rita Brazil 4 Jack UK 5 Ron Spain 6 Jeff China 7 Carrie Italy 8 Marry Peru 9 Ben Russia
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 Documentation.