user11055831
user11055831

Reputation:

convert series to list in python

here is my s, first column is my INDEX(I replace index with column ) not column and second is column

s =              value
      time            
12-3 19:60:00   0.42724
22-4 20:30:00   0.58799
52-4 21:50:00   0.64879
62-5 22:10:00   0.64090
62-4 23:20:00   0.75934

type of this s is pandas.core.series.Series now I want to convert it to the list like this -:

[['12-3 19:60:00', 0.42724],
['22-4 20:30:00', 0.58799],
['52-4 21:50:00', 0.64879],
['62-5 22:10:00', 0.64090],
['62-4 23:20:00', 0.75934]]`

i have done this but not directly first i convert it into Dataframe then I applied this-:

s.reset_index().values.tolist()

if I convert it directly it gives me a result like :

 [[Timestamp('12-3 19:60:00'), 0.42724],
 [Timestamp('22-4 20:30:00'), 0.58799]...]

Upvotes: 1

Views: 4211

Answers (1)

jezrael
jezrael

Reputation: 862511

Convert index to Series first:

s.index = s.index.astype(str)
L = s.reset_index().values.tolist()

Or if dont need modify index of Series:

L = s.rename_axis('a').reset_index().assign(a = lambda x: x.a.astype(str)).values.tolist()

Sample:

print (s)
time
2015-12-03 19:40:00    0.42724
2015-04-22 20:30:00    0.58799
Name: value, dtype: float64

s.index = s.index.astype(str)
L = s.reset_index().values.tolist()
print (L)
[['2015-12-03 19:40:00', 0.42723999999999995], ['2015-04-22 20:30:00', 0.58799]]

L = s.rename_axis('a').reset_index().assign(a = lambda x: x.a.astype(str)).values.tolist()
print (L)
[['2015-12-03 19:40:00', 0.42723999999999995], ['2015-04-22 20:30:00', 0.58799]]

Upvotes: 4

Related Questions