问题描述
我有一个pandas DataFrame,我想从中删除行,其中特定列中字符串的长度大于2.
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)]
但是我只得到错误:
KeyError: u'no item named False'
我在做什么错了?
(注意:我知道我可以使用df.dropna()
除去包含任何NaN
的行,但是我没有看到如何根据条件表达式删除行.)
(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.)
推荐答案
执行len(df['column name'])
时,您只会得到一个数字,即DataFrame中的行数(即列本身的长度).如果要对列中的每个元素应用len
,请使用df['column name'].map(len)
.所以尝试
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]
这篇关于根据涉及给定KeyError的len(string)的条件表达式从 pandas DataFrame删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!