Here is a simple template that you may use to import a CSV file into Python using Pandas:
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 data stored in a CSV file (where the file name is ‘products_sold‘):
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 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:
import pandas as pd df = pd.read_csv(r'C:\Users\Ron\Desktop\products_sold.csv') print(df)
Note that you should place ‘r‘ before the path string to address any special characters in the path, such as ‘\’. Additionally, don’t forget to put the file name at the end of the path + “.csv”
Step 3: Run the Code
Finally, run the Python code and you’ll get:
product brand price 0 Computer A 1200 1 Tablet B 350 2 Printer C 120 3 Monitor D 400 4 Keyboard E 80
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 product and price 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\products_sold.csv') df = pd.DataFrame(data, columns=['product', 'price']) 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 product and price columns:
product price 0 Computer 1200 1 Tablet 350 2 Printer 120 3 Monitor 400 4 Keyboard 80
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.