擅长:python、mysql、java
<p><strong>代码</strong></p>
<pre><code>df = df.set_index('timestamp')
df = df.reindex(np.arange(df.index[0], 1+df.index[-1])).ffill().reset_index().astype(int)
</code></pre>
<p><strong>输出</strong></p>
<pre><code> timestamp v
0 1 5
1 2 6
2 3 6
3 4 6
4 5 6
5 6 7
6 7 7
7 8 8
</code></pre>
<p><strong>解释</strong></p>
<p>我们将索引设置为timestamp,然后根据索引的第一个和最后一个值重新索引,然后向前填充值</p>