Pandas Get a list of index from dataframe.loc
<p>I have looked through various sites and SO posts.Seems easy but somehow i am stuck with this.I am using</p>

<pre><code>print frame.loc[(frame['RR'].str.contains("^[^123]", na=False)), 'RR'].isin(series1.str.slice(1))
</code></pre>

<p>to get</p>

<pre><code>3     True
4    False
8    False
Name: RR, dtype: bool
</code></pre>

<p>Now,somehow i want the <code>indexes</code> only so that i can use that in <code>dataframe.drop</code>. Basically all the indexes where value is <code>True</code> , i have to grab <code>indexes</code> and <code>drop</code> them.Is there any other way as well without using <code>indexes</code>?</p>

<p>You are testing two conditions on the same column so these can be combined (and negated):</p>

<pre><code>frame[~((frame['RR'].str.contains("^[^123]", na=False)) &amp; (frame['RR'].isin(series1.str.slice(1))))]
</code></pre>

<p>Here, after <code>~</code> operator, it checks whether a particular row satisfies both conditions - same as the boolean array you get in the end. With <code>~</code>, you turn True's to False's and False's to True's. Finally, <code>frame[condition]</code> returns the rows that satisfy the final condition with boolean indexing.</p>

<p>In a more readable format:</p>

<pre><code>condition1 = frame['RR'].str.contains("^[^123]", na=False)
condition2 = frame['RR'].isin(series1.str.slice(1))
frame[~(condition1 &amp; condition2)]
</code></pre>

<p>As an alternative (requires 0.18.0), you can get the indices of the True elements with:</p>

<pre><code>frame.loc[(frame['RR'].str.contains("^[^123]", na=False)), 'RR'].isin(series1.str.slice(1))[lambda df: df].index
</code></pre>

python python-2.7 pandas