0

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!

David
  • 51
  • 3
  • Does this answer your question? [Filling the missing index and filling its value with 0](https://stackoverflow.com/questions/50690963/filling-the-missing-index-and-filling-its-value-with-0) – Ynjxsjmh Jul 10 '22 at 15:18

1 Answers1

1

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
rhug123
  • 7,893
  • 1
  • 9
  • 24