In this tutorial, we will learn how to read multiple columns from a CSV file in Python. CSV (Comma Separated Values) is a simple file format that is widely supported by various applications, including Microsoft Excel and Google Sheets, to store and organize data. Python provides built-in support for reading and writing CSV files using the popular csv
module.
Step 1: Preparing the CSV File
For this tutorial, let’s assume you have a sample CSV file named sample.csv
with the following content:
Name,Age,Occupation,Salary Alice,30,Software Developer,90000 Bob,35,Data Analyst,75000 Cathy,28,Web Developer,80000 David,45,Project Manager,120000
In this file, we have four columns: Name, Age, Occupation, and Salary.
Step 2: Importing the Required Libraries
To work with CSV files in Python, we will need to import the csv
module. If you want to work with numerical data, you might also want to import the numpy
module. In this tutorial, we will use only the csv
module.
1 |
import csv |
Step 3: Reading the CSV File
To read a CSV file, we will use the csv.reader
function. This function takes an iterable, such as a file object, and returns a reader object that iterates through the rows in the CSV file.
1 2 3 4 |
with open('sample.csv', mode='r') as file: csv_reader = csv.reader(file, delimiter=',') for row in csv_reader: print(row) |
The above code snippet will read and print the entire CSV file as a list of lists in this format:
['Name', 'Age', 'Occupation', 'Salary'] ['Alice', '30', 'Software Developer', '90000'] ['Bob', '35', 'Data Analyst', '75000'] ['Cathy', '28', 'Web Developer', '80000'] ['David', '45', 'Project Manager', '120000']
Step 4: Reading Multiple Columns from the CSV File
Now that we know how to read the entire CSV file, let’s read only specific columns. In this example, we will read Name and Occupation columns from the CSV file.
We can achieve this by accessing the indices of the desired columns in each row of the CSV file.
1 2 3 4 5 6 7 8 |
with open('sample.csv', mode='r') as file: csv_reader = csv.reader(file, delimiter=',') next(csv_reader) # Skipping the header row for row in csv_reader: name = row[0] occupation = row[2] print(name, occupation) |
The above code snippet will only print the Name and Occupation columns from the CSV file:
Alice Software Developer Bob Data Analyst Cathy Web Developer David Project Manager
Full Code
Here is the complete code for reading multiple columns from a CSV file:
1 2 3 4 5 6 7 8 9 10 |
import csv with open('sample.csv', mode='r') as file: csv_reader = csv.reader(file, delimiter=',') next(csv_reader) # Skipping the header row for row in csv_reader: name = row[0] occupation = row[2] print(name, occupation) |
Conclusion
In this tutorial, we learned how to read multiple columns from a CSV file in Python using the csv
module. This is a simple and efficient method to process and manipulate data from a CSV file. You can now use this knowledge to read data from CSV files and perform further analysis, visualizations, or transformations using Python.