I have this code to drop null values from column Type, specifically looking at Dog.
cd.loc[cd['Type'] == 'Dog'].dropna(subset = ['Killed'], inplace = True)
I would like to dropna when the ['Killed'] column associating with Type = Dog has NaN value.
The code above generate this pandas error:
 A value is trying to be set on a copy of a slice from a DataFrame
Is there another way where can I dropna on ['Killed'] when ['Type'] == 'Dog'?
(This is my first post), sorry if I can't explain properly Cheers
To drop all the rows with the NaN values, you may use df. dropna().
We can use the column_name function along with the operator to drop the specific value.
Pandas Drop All Rows with any Null/NaN/NaT Values This is the default behavior of dropna() function.
Very similar to @BrenBarn's answer but using drop and inplace
cd.drop(cd[(cd.Type == 'Dog') & (cd.Killed.isnull())].index, inplace=True)
cd = pd.DataFrame([
        ['Dog', 'Yorkie'],
        ['Cat', 'Rag Doll'],
        ['Cat', None],
        ['Bird', 'Caique'],
        ['Dog', None],
    ], columns=['Type', 'Killed'])
cd.drop(cd[(cd.Type == 'Dog') & (cd.Killed.isnull())].index, inplace=True)
cd

Equivalently with DeMorgan's law
cond1 = cd.Type == 'Dog'
cond2 = cd.Killed.isnull()
cd[~cond1 | ~cond2]
A silly one, because I felt like it!
cd.groupby('Type', group_keys=False) \
    .apply(lambda df: df.dropna(subset=['Killed']) if df.name == 'Dog' else df)
                        It sounds like what you are saying is you want to remove rows where Type is "Dog" and Killed is NaN.  So just select the negation of that condition:
cd = cd.loc[~((cd.Type=="Dog") & cd.Killed.isnull())]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With