Search code examples
pythonpandasdictionarytuplesseries

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?


Solution

  • 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