简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM