如何根据条件表达式从 Pandas DataFrame 中删除行 [英] How to delete rows from a pandas DataFrame based on a conditional expression
问题描述
我有一个 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]
这篇关于如何根据条件表达式从 Pandas DataFrame 中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!