How to Return a Column of a pandas DataFrame as a list in Python
To get a column from a pandas DataFrame and convert it into a list, use the pandas.DataFrame.tolist()
method.
To demonstrate how to do this, let's create a new pandas DataFrame with some data then get one of the columns by its name.
import pandas
data = {
'name': ['john', 'amy', 'ben'],
'age': [29, 22, 30]
}
data_frame = pandas.DataFrame(data)
name_list = data_frame['name'].tolist()
print(name_list)
print(type(name_list))
['john', 'amy', 'ben']
<class 'list'>