Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to create a list from an excel column

Tags:

pandas

I have a list of names in one column of a csv file. I'm trying to make this into a list in python that looks like

list = ['name1', 'name2', 'name3']

and so on.

I have the following

import pandas as pd
export = pd.read_csv('Top100.csv', header=None)

but I can't figure out how to pull out the information and put it into a list format.

like image 319
Pie Junkie Avatar asked Sep 13 '26 21:09

Pie Junkie


1 Answers

The below is applicable if your data is in a vertical column

export = pd.read_csv('Top100.csv', header=None)
export.values.T[0].tolist()

The .T in this transposes the values, as normally pandas is row oriented. Then you take the [0] index because Pandas reads excel or csv sheets in as a matrix, even if there's only a single column. Call the tolist() method on it and you're done.

like image 88
Greg Jennings Avatar answered Sep 16 '26 23:09

Greg Jennings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!