Pandas DataFrame with continuous index
<p>I have the following code:</p>

<pre><code>import pandas as pd
df = pd.DataFrame(
    {'Index' : ['1', '2', '5','7', '8', '9', '10'],
     'Vals' : [1, 2, 3, 4, np.nan, np.nan, 5]})
</code></pre>

<p>This gives me:</p>

<pre><code>  Index  Vals
0     1   1.0
1     2   2.0
2     5   3.0
3     7   4.0
4     8   NaN
5     9   NaN
6    10   5.0
</code></pre>

<p>But what I want is something like this:</p>

<pre><code>  Index      Vals
0     1  1.000000
1     2  2.000000
2     3  NaN
3     4  NaN
4     5  3.000000
5     6  NaN
6     7  4.000000
7     8  NaN
8     9  NaN
9    10  5.000000
</code></pre>

<p>I tried to achieve this by creating a new dataframe with a continuous index. Then I would like to assign the values which I already have but how? The only thing I have so far is this:</p>

<pre><code>clean_data = pd.DataFrame({'Index' : range(1,11)})
</code></pre>

<p>Which gives me:</p>

<pre><code>   Index
0      1
1      2
2      3
3      4
4      5
5      6
6      7
7      8
8      9
9     10
</code></pre>

<p>So for your example it will look like:</p>

<pre><code>import pandas as pd
import numpy as np 

df = pd.DataFrame(
    {'Index' : ['1', '2', '5','7', '8', '9', '10'],
     'Vals' : [1, 2, 3, 4, np.nan, np.nan, 5]})
df['Index'] = df['Index'].astype(int)
clean_data = pd.DataFrame({'Index' : range(1,11)})
result = clean_data.merge(df,on="Index",how='outer')
</code></pre>

<p>And the result is : </p>

<pre><code>  Index Vals
0   1   1.0
1   2   2.0
2   3   NaN
3   4   NaN
4   5   3.0
5   6   NaN
6   7   4.0
7   8   NaN
8   9   NaN
9   10  5.0
</code></pre>

python pandas indexing