pandas 如何根据条目的长度过滤熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40006276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to filter a pandas dataframe based on the length of a entry
提问by Dan Steingart
In a pandas dataframe I have a field 'amp' that should be populated by a list of length 495. Is there a panda-ic way to quickly filter on this length, such that all rows with field 'amp' are not equal to 495 are dropped?
在Pandas数据框中,我有一个字段“amp”,它应该由长度为 495 的列表填充。是否有一种Pandas式方法可以快速过滤此长度,以便所有带有字段“amp”的行不等于 495被丢弃?
I tried
我试过
df[len(df['amp']) == 495]
and this returned
这又回来了
KeyError: False
Thanks in advance.
提前致谢。
回答by SethMMorton
If you specifically need len
, then @MaxU's answer is best.
如果您特别需要len
,那么@MaxU 的答案是最好的。
For a more general solution, you can use the mapmethod of a Series.
对于更通用的解决方案,您可以使用系列的map方法。
df[df['amp'].map(len) == 495]
This will apply len
to each element, which is what you want. With this method, you can use any arbitrary function, not just len
.
这将适用len
于您想要的每个元素。使用此方法,您可以使用任意函数,而不仅仅是len
.
回答by MaxU
Try this:
尝试这个:
df[df['amp'].str.len() == 495]
Demo:
演示:
In [77]: df
Out[77]:
a
0 [1, 2, 3, 4, 5]
1 [1, 2, 3]
2 [-1]
In [78]: df.a.str.len()
Out[78]:
0 5
1 3
2 1
Name: a, dtype: int64
In [79]: df[df.a.str.len() == 3]
Out[79]:
a
1 [1, 2, 3]