To import a CSV file into Python using Pandas, you can use the read_csv() function as shown below:
import pandas as pd
df = pd.read_csv(r"Full\Path\To\Your\Folder\sample_data.csv")
print(df)
The Example
To start with a simple illustration, suppose you have the following data saved in a CSV file (where the file name is “inventory_list”):
| product | brand | price |
|---|---|---|
| Laptop | X | 1350 |
| Smartphone | Y | 650 |
| Scanner | Z | 220 |
| Display | Q | 480 |
| Mouse | R | 45 |
This CSV file contains three columns: product name, brand, and price.
Steps to Import a CSV File into Python using Pandas
Step 1: Capture the File Path
First, identify and copy the full file path where your CSV file is stored on your computer.
For example, assume that the CSV file is saved at:
C:\Users\Alex\Documents\inventory_list.csv
You will need to modify your Python script so that the path reflects the actual location of your file. Be sure to include:
- The file name – It must exactly match the name of your CSV file.
- The file extension – When importing CSV files, the extension must be
.csv. - The complete directory structure – Any missing folder name will cause an error.
Even a small mismatch in the file name or extension can prevent Python from locating the file.
Step 2: Apply the Python Code
Enter the following code into your Python environment (such as Jupyter Notebook, VS Code, or any other IDE). Update the file path to match your system:
import pandas as pd
df = pd.read_csv(r"C:\Users\Alex\Documents\inventory_list.csv")
print(df)
Important points to remember:
- The letter
rbefore the path string creates a raw string, which prevents issues with backslashes (\) in Windows file paths. - Always include the file name followed by
.csvat the end of the path. - Make sure that the Pandas library is installed in your Python environment before running the script.
Step 3: Run the Code
After updating the file path correctly, run the code. If everything is set up properly, you should see an output similar to this:
product brand price
0 Laptop X 1350
1 Smartphone Y 650
2 Scanner Z 220
3 Display Q 480
4 Mouse R 45
Pandas automatically assigns an index starting from 0. The data from the CSV file is loaded into a DataFrame, which is a structured tabular object that allows further data manipulation and analysis.
Optional Step: Select Subset of Columns
In some situations, you may only need specific columns from the CSV file rather than the entire dataset.
For example, suppose you want to select only the product and price columns. You can specify the column names as shown below:
import pandas as pd
data = pd.read_csv(r"C:\Users\Alex\Documents\inventory_list.csv")
df = pd.DataFrame(data, columns=["product", "price"])
print(df)
Make sure that the column names written inside the list exactly match the column headers in the CSV file. If there is any spelling mistake or difference in capitalization, Pandas may return NaN values or raise an error.
Once executed, the output would display only the selected columns:
product price
0 Laptop 1350
1 Smartphone 650
2 Scanner 220
3 Display 480
4 Mouse 45
Additional Resources
You have now seen how to import a CSV file into Python using Pandas. This is a fundamental operation in data analysis and automation tasks.
After importing your data, you can begin performing calculations, filtering records, summarizing statistics, cleaning data, or exporting the DataFrame back into a CSV file.
Becoming comfortable with file imports is an essential step toward working efficiently with data in Python.
See also: How to Import an Excel File into Python using Pandas
