user3841581
user3841581

Reputation: 2747

converting rows of a single dataFrame column into a single list in python

I have a pandas Series and I would like to covert all the rows of my series into a single list. I have:

list1=Series([89.34,88.52,104.19,186.84,198.15,208.88]). Then I have a function which i call:
func(list1)
def func(list1):
   list1=list1.values.tolist()
   print(list1)

the printed result is :

[[89.34], [88.52], [104.19], [186.84], [198.15], [208.88]]

but I would like to have : [89.34,88.52,104.19,186.84,198.15,208.88]

any help. I am using python 2.7

Upvotes: 0

Views: 364

Answers (1)

Ray
Ray

Reputation: 223

Easiest way would be to access the values property of the Series object, like you have in your example.

> from pandas import Series
> a_series = Series([89.34,88.52,104.19,186.84,198.15,208.88])
> print(a_series.values.tolist())
[89.34, 88.52, 104.19, 186.84, 198.15, 208.88]

Accessing more than one elements in the list at the same time:

> counter = 0
> a_list = a_series.values.tolist()
> while(counter < len(a_list)):
..    if((counter+1) < len(a_list)):
..        print(a_list[counter] == a_list[counter+1])
..    counter+=1
False
False
False
False
False

Upvotes: 1

Related Questions