python logo

csv to array python


Python hosting: Host, run, and code Python in the cloud!

CSV (Comma-Separated Values) files represent a standard format for storing tabular data in plain text. In this guide, we’ll dive deep into how to handle and read CSV files using Python.

What is a CSV File?
A CSV file is essentially a table of values where each value is separated by a comma, hence the name. This format is widely recognized and utilized for data exchange, storage, and processing. Many tools like Excel can interpret and display CSV data in a spreadsheet format. In the Python ecosystem, there are a couple of modules, like csv and pandas, that facilitate working with CSV files.

Sample CSV Data
To illustrate, consider a simple CSV file representing contact details:

1
2
3
"first_name","last_name","email","address","city","state","zip","phone"
"charlie","davidson","[email protected]","123 main street, akron, ohio","akron, ohio","23678"
"tanya","jones","[email protected]", "734 main street", "ny", "new york", "nyc", "12354"

And another CSV file, this time showing dates and numbers:

1
2
3
4
01/01/2016, 4
02/01/2016, 2
03/01/2016, 10
04/01/2016, 8

Basic CSV Reading in Python
Reading CSV files in Python is straightforward. Begin by importing the csv module, and then use its reader function to process the file.

1
2
3
4
5
6
import csv

with open('file.csv') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
print(row)

Each row from the CSV file is represented as a list in Python. So, row[0] would give you the first column value, row[1] the second, and so on.

Better Data Handling: Storing in Arrays
For more intuitive data access, it’s beneficial to store CSV columns in separate arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
import csv

dates = []
scores = []

with open('file.csv') as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
dates.append(row[0])
scores.append(row[1])

print(dates)
print(scores)

Creating a CSV Reading Function
If you have multiple CSV files with identical formats, encapsulate the reading logic in a function to avoid code duplication.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import csv

def readMyFile(filename):
dates = []
scores = []
with open(filename) as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
dates.append(row[0])
scores.append(row[1])
return dates, scores

dates,scores = readMyFile('file.csv')
print(dates)
print(scores)

Advanced CSV Handling with Pandas
While the csv module is handy, pandas provides a more powerful way to handle CSV files. The key function here is read_csv.

1
2
3
4
import pandas as pd

df = pd.read_csv('data.csv')
print(df.to_string())

In pandas, data is usually stored in a structure called a DataFrame. This offers a lot of utility functions and methods to facilitate complex data manipulations.

To fetch a particular row from the DataFrame, use:

1
print(df.loc[0])

Conclusion
In this article, we’ve seen the versatility and power Python offers when dealing with CSV files, be it through the csv module or the advanced pandas library. The choice of approach largely depends on your specific needs and the complexity of the data you’re dealing with.

Further Learning
Dive deeper into Python programming with this comprehensive Python Programming Bootcamp.

Continue to the Next Tutorial






Leave a Reply: