3

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Read csv will return a pandas dataframe so your columns can be accessed through the dataframe. Say your file has columns "A", "B", "C"

import pandas as pd
data = pd.read_csv('Top100.csv', header=None)
print data["a"]

Or as a list

print list(data["a"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.