I have a data set that consists of two columns as coordinates. I would like to import them to python under one header list called "coordinates". Does anyone have any solution?. Thanks!
Asked
Active
Viewed 197 times
0
-
https://stackoverflow.com/questions/24662571/python-import-csv-to-list – sharathnatraj Nov 25 '20 at 01:48
-
This query is about how to import csv as a list. I would like to combine two columns under one header. – amany marey Nov 25 '20 at 01:51
2 Answers
1
You can use: df.values.tolist()
.
First select the columns from the name of your dataframe:
df = pd.DataFrame(name_of_dataframe, columns= ['col1', 'col2'])
Next change it to a list: list = df.values.tolist()
.
Then flatten the list with a loop appending each smaller list into one bigger list.

triedit
- 169
- 1
- 9
1
You can use list comprehension with csv module.
import csv
with open('filename.csv') as file:
coordinates = [(float(x), float(y)) for x, y in csv.reader(file, delimiter= ',')]

Shivam Miglani
- 542
- 3
- 9