0

I have this series:

ser = pd.Series(data=[11, 22, 33, 44, 88], index=[0, 1, 2, 3, 7])
ser
0    11
1    22
2    33
3    44
7    88

I want to convert it to a list so I do the following and result is:

ser.tolist()
result: [11, 22, 33, 44, 88]

However, what I want is a list where each element is inserted at the index it has in series:

[11, 22, 33, 44, 0, 0, 0, 88]

How can I achieve this?

Hoori M.
  • 700
  • 1
  • 7
  • 21
  • 1
    Related: [Fill missing index with 0's](https://stackoverflow.com/questions/50690963/filling-the-missing-index-and-filling-its-value-with-0) – noah Feb 05 '21 at 01:18

1 Answers1

3

Try with reindex

ser.reindex(range(ser.index.max()+1),fill_value=0).tolist()
Out[13]: [11, 22, 33, 44, 0, 0, 0, 88]
BENY
  • 317,841
  • 20
  • 164
  • 234