在 Pandas 中,如何根据值的类型过滤系列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46999146/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:42:49  来源:igfitidea点击:

In Pandas, how to filter a Series based on the type of the values?

pythonpandas

提问by Kurt Peek

Given a Serieslike

给个Series

import pandas as pd

s = pd.Series(['foo', 'bar', 42])

I would like to obtain a 'sub-series' pd.Series(['foo', 'bar'])in which all values are strings. I've tried Boolean indexing like so:

我想获得一个“子系列” pd.Series(['foo', 'bar']),其中所有值都是字符串。我试过这样的布尔索引:

s[isinstance(s, str)]

but this gives a

但这给出了一个

KeyError: False

关键错误:错误

In my search for suitable methods so far I came across select, but this imposes a criterion on the labels, not the values. How can I filter based on (the type of) the values in this case?

到目前为止,我在寻找合适的方法时遇到了select,但这对标签而不是值强加了一个标准。在这种情况下,如何根据值(的类型)进行过滤?

回答by jezrael

Use applyor list comprehension:

使用apply或列出理解:

s[s.apply(lambda x: isinstance(x, str))]

Same as, thanks Jon Clements?:

一样,谢谢Jon Clements?

s[s.apply(isinstance, args=(str,))]


s[[isinstance(x, str) for x in s]]

All return:

全部返回:

0    foo
1    bar
dtype: object

EDIT:

编辑:

This is not recommended, thanks c???s????:

这是不推荐的,谢谢c???s????

s[s.apply(type) == str]

回答by cs95

A little trick with pd.to_numeric:

一个小技巧pd.to_numeric

s[pd.to_numeric(s, errors='coerce').isnull()]

0    foo
1    bar
dtype: object

If an item is numeric, it is successfully coerced (not NaN) and so is dropped from the final result.

如果一个项目是数字,它被成功强制(不是NaN),因此从最终结果中删除。

回答by Zero

I'd use pd.to_numericas pointed above.

我会pd.to_numeric像上面指出的那样使用。

Alternatively, you can use str.isalpha

或者,您可以使用 str.isalpha

In [109]: s[s.str.isalpha().notnull()]
Out[109]:
0    foo
1    bar
dtype: object