Hoori M.
Hoori M.

Reputation: 730

Converting Pandas Series to a List with indices preserved

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?

Upvotes: 0

Views: 391

Answers (1)

BENY
BENY

Reputation: 323396

Try with reindex

ser.reindex(range(ser.index.max()+1),fill_value=0).tolist()
Out[13]: [11, 22, 33, 44, 0, 0, 0, 88]

Upvotes: 3

Related Questions