Posted By: Anonymous
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.)
When you do
len(df['column name']) you are just getting one number, namely the number of rows in the DataFrame (i.e., the length of the column itself). If you want to apply
len to each element in the column, use
df['column name'].map(len). So try
df[df['column name'].map(len) < 2]