if I have a Series
JavaScript
x
2
1
s = pd.Series(1, index=[1,2,3,5,6,9,10])
2
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
JavaScript
1
2
1
s = pd.Series([1,1,1,0,1,1,0,0,1,1], index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
2
How should I update the series? Thank you in advance!
Advertisement
Answer
Try this:
JavaScript
1
2
1
s.reindex(range(1,s.index.max() + 1),fill_value=0)
2
Output:
JavaScript
1
11
11
1
1 1
2
2 1
3
3 1
4
4 0
5
5 1
6
6 1
7
7 0
8
8 0
9
9 1
10
10 1
11