K.stambuli
K.stambuli

Reputation: 1

Python 2.7 Join two values as list in same row of two different columns (from csv file)

I have a .csv file with two columns of interest 'latitude' and 'longitude' with populated values

I would like to return [latitude, longitude] pairs of each row from the two columns as lists...

[10.222, 20.445] [10.2555, 20.119] ... and so forth for each row of my csv...

The problem with > import pandas colnames = [ 'latitude', 'longitude'] data = pandas.read_csv('path_name.csv', names=colnames)

latitude = data.latitude.tolist()
longitude = data.longitude.tolist()

is that it creates two lists for all the values each latitude and longitude column

How can I create lists for each latitude, longitude pair in python ?

Upvotes: 0

Views: 553

Answers (2)

Marjorie Pickard
Marjorie Pickard

Reputation: 109

So from what I understand is that you want many lists of two elements: lat and long. However what you are receiving is two lists, one of lat and one of long. what I would do is loop over the length of those lists and then take that element in the lat/long lists and put them together in their own list.

for x in range(0, len(latitude)):
    newList = [latitude[x], longitude[x]]

Upvotes: 0

Prasad Honavar
Prasad Honavar

Reputation: 117

Most basic way

import csv
with open('filename.txt', 'r') as csvfile:
     spamreader = csv.reader(csvfile)
     for row in spamreader:
         print row

Upvotes: 1

Related Questions