问题来源:https://stackoverflow.com/que…问:我有一个pandas DataFrame,我想删除它特定列中字符串差姑娘是大于2的行,我知道我可以使用df.dropna()来去除包含NaN的行,但我没有找到如何根据条件删除行。似乎我能够这样做:df[(len(df[‘column name’]) < 2)]但却报错了:KeyError: u’no item named False’谁能告诉我错在哪里了?回答一:当你这样做时,len(df[‘column name’])你只得到一个数字,即DataFrame中的行数(即列本身的长度)。如果要应用于len列中的每个元素,请使用df[‘column name’].map(len)。尝试使用:df[df[‘column name’].map(len) < 2]评论:我想出了一种使用列表解析的方法:df[[(len(x) < 2) for x in df[‘column name’]]] 但是你这种方法更好些。回答二:要直接回答这个问题,一种方法是使用drop方法:df = df.drop(some labels)df = df.drop(df[<some boolean condition>].index)要删除列“score”<50的所有行:df = df.drop(df[df.score < 50].index)替换版本df.drop(df[df.score < 50].index, inplace=True)多条件情况:可以使用操作符: | 只需其中一个成立, & 同时成立, ~ 表示取反,它们要用括号括起来。例如删除列“score<50 和>20的所有行df = df.drop(df[(df.score < 50) & (df.score > 20)].index)