I have a dataframe that looks like this:
B
A
0.00 5.7096
7.33 8.0280
25.82 15.7212
43.63 19.5156
55.24 20.1888
and I want to add rows with the index at regular intervals (say by 10), so that I can then interpolate the column B with method = 'index'. My desired output is this:
B
A
0.00 5.7096
7.33 8.0280
10.00 NaN
20.00 NaN
25.82 15.7212
30.00 NaN
40.00 NaN
43.63 19.5156
50.00 NaN
55.24 20.1888
60.00 NaN
I haven't found any reindex option that adds index elements instead of changing them. My best solution is create a new index, append it to the original dataframe, sort and remove duplicates (if any), but I'm pretty sure there is a better solution.
step = 10
idx = pd.DataFrame(index = df.index).reindex([round(i, 0) for i in np.arange(df.index[0], df.index[-1] + step, step)])
df = df.append(idx)
df.sort_index(inplace = True)
df = df[~df.index.duplicated()]
any suggestion? thanks