0

I have dataframe in which I have to drop row if some of values.

for instance,

x not in ['N/A', ''] where x is columns

is there a way like, apply?

 df[x] = df[x].apply(lambda x: x.lower())

I am think in something like:

df.drop.apply(lambda x: X not in ['N/A', ''])???

My DF

     F   T   l
0    0   "0"   "0"
1    1   ""   "1"
2    2   "2"   ""

drop row if T == "" or l == ""

     F   T   l
0    0   "0"   "0"

I could not use

df.drop(df.T == "") since the condition ("") depend on runtime data
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Agus
  • 1,604
  • 2
  • 23
  • 48

2 Answers2

0

If you are looking to remove any row that has 'N/A' or '' in the row then you can us a boolean index, just take the inverse of isin() e.g.:

In []:
df[~df.isin(['N/A', '']).any(axis=1)]

Out[]:
   F  T  l
0  0  0  0

If you need to limit to just columns 'A', 'l' then select them, e.g.:

df[~df[['A', 'l']].isin(['N/A', '']).any(axis=1)]

You could also use a dict with isin() but that would only be useful if you had different values for the columns, e.g.:

df[~df.isin({'A': ['N/A', ''], 'l': ['']}).any(axis=1)]
AChampion
  • 29,683
  • 4
  • 59
  • 75
0

From the following answer, the solution is:

 mask = df.pipe(lambda x: (x['T'].isin(['N/A', ''])) | (x['T'].isna()),)
 df.drop(df[mask].index, inplace=True)

this allow to provide different lambdas

Agus
  • 1,604
  • 2
  • 23
  • 48