Conditional dropna() pandas

Viewed 6531

I have a rather simple question: I want to drop rows from a DataFrame based on a condition. The DataFrame looks something like this:

Program        act
Original RO     A
Original RO    nan
Followup RO     B
Followup RO    nan
Integral RO    nan

I want to delete nulls for Original RO and Integral RO Programs only. So it should look like this:

Program        act
Original RO     A
Followup RO     B
Followup RO    nan

I always have problems when trying to do some operations on a portion of my dataframe and keep the rest as is.

I tried this:

df.loc[df.Program.str.match('^(Original|Integral)')] = df.dropna()

But isn't working. What am I doing wrong? Thanks in advance!

2 Answers

You can check conditions with isin() and isna() and subset the dataframe.

lst = ['Original RO', 'Integral RO']

df = df[~(df['Program'].isin(lst) & df['act'].isna())]

# Another one using drop.
# df = df.drop(df[(df['Program'].isin(lst) & df['act'].isna())].index)

print (df)

       Program  act
0  Original RO    A
2  Followup RO    B
3  Followup RO  NaN

You can use the parameter subset in the call to dropna():

df.dropna(subset=cols, inplace=True)

where cols is the name of column you want to check (to drop the line).

Related