Search code examples
pythonpandasindexingseries

Pandas data frame index


if I have a Series

s = pd.Series(1, index=[1,2,3,5,6,9,10])

But, I need a standard index = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], with index[4, 7, 8] values equal to zeros. So I expect the updated series will be

s = pd.Series([1,1,1,0,1,1,0,0,1,1], index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

How should I update the series? Thank you in advance!


Solution

  • Try this:

    s.reindex(range(1,s.index.max() + 1),fill_value=0)
    

    Output:

    1     1
    2     1
    3     1
    4     0
    5     1
    6     1
    7     0
    8     0
    9     1
    10    1