Index values of specific rows in python
<p>I am trying to find out Index of such rows before "None" occurs.</p>

<pre><code>pId=["a","b","c","None","d","e","None"]
df = pd.DataFrame(pId,columns=['pId'])

              pId
0               a
1               b
2               c
3            None
4               d
5               e
6            None

df.index[df.pId.eq('None') &amp; df.pId.ne(df.pId.shift(-1))]
</code></pre>

<p>I am expecting the output of the above code should be</p>

<pre><code>Index([2,5])
</code></pre>

<p>It gives me </p>

<pre><code>Index([3,6])
</code></pre>

<p>Please correct me</p>

<p>Just <code>-1</code> from <code>df[df["pId"] == "None"].index</code>:</p>

<pre><code>import pandas as pd

pId=["a","b","c","None","d","e","None"]
df = pd.DataFrame(pId,columns=['pId'])

print(df[df["pId"] == "None"].index - 1)
</code></pre>

<p>Which gives you:</p>

<pre><code>Int64Index([2, 5], dtype='int64')
</code></pre>

<p>Or if you just want a list of values:</p>

<pre><code>(df[df["pId"] == "None"].index - 1).tolist()
</code></pre>

<p>You should be aware that for a list like:</p>

<pre><code>pId=["None","None","b","c","None","d","e","None"]
</code></pre>

<p>You get a df like:</p>

<pre><code>    pId
0  None
1  None
2     b
3     c
4  None
5     d
6     e
7  None
</code></pre>

<p>And output like:</p>

<pre><code>[-1, 0, 3, 6]
</code></pre>

<p>Which does not make a great deal of sense.</p>

python pandas indexing shift