How to delete rows from a pandas DataFrame based on a conditional expression [duplicate]

I have a pandas DataFrame and I want to delete rows from it where the length of the string in a particular column is greater than 2.

I expect to be able to do this (per this answer):

df[(len(df['column name']) < 2)]

but I just get the error:

KeyError: u'no item named False'

What am I doing wrong?

(Note: I know I can use df.dropna() to get rid of rows that contain any NaN, but I didn’t see how to remove rows based on a conditional expression.)

6 s
6

Leave a Comment