Pandas .dropna() on specify attribute
<p>I have this code to drop null values from column Type, specifically looking at Dog.</p>

<pre><code>cd.loc[cd['Type'] == 'Dog'].dropna(subset = ['Killed'], inplace = True)
</code></pre>

<p>I would like to dropna when the ['Killed'] column associating with Type = Dog has NaN value.</p>

<p>The code above generate this pandas error:</p>

<pre><code> A value is trying to be set on a copy of a slice from a DataFrame
</code></pre>

<p>Is there another way where can I dropna on ['Killed'] when ['Type'] == 'Dog'?</p>

<p>(This is my first post), sorry if I can't explain properly
Cheers</p>

<p>Very similar to @BrenBarn's answer but using <code>drop</code> and <code>inplace</code></p>

<pre><code>cd.drop(cd[(cd.Type == 'Dog') &amp; (cd.Killed.isnull())].index, inplace=True)
</code></pre>

<h3>Setup</h3>

<pre><code>cd = pd.DataFrame([
        ['Dog', 'Yorkie'],
        ['Cat', 'Rag Doll'],
        ['Cat', None],
        ['Bird', 'Caique'],
        ['Dog', None],
    ], columns=['Type', 'Killed'])
</code></pre>

<h3>Solution</h3>

<pre><code>cd.drop(cd[(cd.Type == 'Dog') &amp; (cd.Killed.isnull())].index, inplace=True)

cd
</code></pre>

<p><a href="http://i.stack.imgur.com/2pPHv.png" rel="nofollow"><img src="http://i.stack.imgur.com/2pPHv.png" alt="enter image description here"></a></p>

<hr>

<p>Equivalently with DeMorgan's law</p>

<pre><code>cond1 = cd.Type == 'Dog'
cond2 = cd.Killed.isnull()
cd[~cond1 | ~cond2]
</code></pre>

<hr>

<p>A silly one, because I felt like it!</p>

<pre><code>cd.groupby('Type', group_keys=False) \
    .apply(lambda df: df.dropna(subset=['Killed']) if df.name == 'Dog' else df)
</code></pre>

python pandas