I'm trying to fill out the missing indices with 0. I've tried some code from similar questions, but it didn't really do the trick fully.
I have a DataFrame:
df
2 1
5 5
When I use: df.reindex(range(df.index[0], df.index[-1] 1), fill_value=0)
, I get:
df
2 1
3 0
4 0
5 5
I expect:
df
0 0
1 0
2 1
3 0
4 0
5 5
CodePudding user response:
Try reindex
with np.arange
:
>>> df.reindex(np.arange(df.index[-1] 1), fill_value=0)
0 0
1 0
2 1
3 0
4 0
5 5
dtype: int64
>>>
CodePudding user response:
You can use
df.reindex(range(df.index.max() 1), fill_value=0)
CodePudding user response:
After np.arange
and range
, the last version with pd.RangeIndex
:
>>> df.reindex(pd.RangeIndex(df.index.max() 1), fill_value=0)
0 0
1 0
2 1
3 0
4 0
5 5
dtype: int64