Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a list of tuples to a Pandas series

I have a list of tuples which I want to convert to a Series.

return array2

[(0, 0.07142857142857142),
  (0, 0.07142857142857142),
  (1, 0.08333333333333333),
  (1, 0.3333333333333333),
  (1, 0.3333333333333333),
  (1, 0.08333333333333333),
  (3, 0.058823529411764705),
  (3, 0.058823529411764705)]

I attempt to do this by converting the list to a dictionary and then to a Series:

 a = pd.Series(dict(array2))

The resulting Series however, doesn't behave as I need it to. It seems to drop key:value pairs (possibly arbitrarily?)

E.g.

return a

 0    0.071429
 1    0.083333
 3    0.058824

How would I obtain a series without dropping any key value pairs?

like image 477
ZakS Avatar asked Nov 18 '18 17:11

ZakS


2 Answers

Using zip and sequence unpacking:

idx, values = zip(*L)

a = pd.Series(values, idx)

With duplicate indices, as in your data, dict will not help as duplicate dictionary keys are not permitted: dict will only take the last value for every key supplied.

like image 138
jpp Avatar answered Oct 07 '22 21:10

jpp


Use DataFrame constructor with set_index by first column, then select second column for Series:

a = pd.DataFrame(array2).set_index(0)[1]
print (a)
0
0    0.071429
0    0.071429
1    0.083333
1    0.333333
1    0.333333
1    0.083333
3    0.058824
3    0.058824
Name: 1, dtype: float64

Or create 2 lists and pass to Series contructor:

idx = [x[0] for x in array2]
vals = [x[1] for x in array2]

a = pd.Series(vals, index=idx)
print (a)
0    0.071429
0    0.071429
1    0.083333
1    0.333333
1    0.333333
1    0.083333
3    0.058824
3    0.058824
dtype: float64
like image 40
jezrael Avatar answered Oct 07 '22 21:10

jezrael