pandas 熊猫只从数据框中选择数字或整数字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24883503/
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
Pandas select only numeric or integer field from dataframe
提问by franco_b
I have this Pandas dataframe (df):
我有这个Pandas数据框(df):
A B
0 1 green
1 2 red
2 s blue
3 3 yellow
4 b black
A type is object.
类型是对象。
I'd select the record where A value are integer or numeric to have:
我会选择 A 值为整数或数字的记录:
A B
0 1 green
1 2 red
3 3 yellow
Thanks
谢谢
回答by EdChum
Call applyon the dataframe (note the double square brackets df[['A']]rather than df['A']) and call the string method isdigit(), we then set param axis=1to apply the lambda function row-wise. What happens here is that the index is used to create a boolean mask.
调用apply数据框(注意双方括号df[['A']]而不是df['A'])并调用字符串方法isdigit(),然后我们设置 paramaxis=1以逐行应用 lambda 函数。这里发生的是索引用于创建布尔掩码。
In [66]:
df[df[['A']].apply(lambda x: x[0].isdigit(), axis=1)]
Out[66]:
A B
Index
0 1 green
1 2 red
3 3 yellow
Update
更新
If you're using a version 0.16.0or newer then the following will also work:
如果您使用的是0.16.0或更高版本,则以下内容也适用:
In [6]:
df[df['A'].astype(str).str.isdigit()]
Out[6]:
A B
0 1 green
1 2 red
3 3 yellow
Here we cast the Series to strusing astypeand then call the vectorised str.isdigit
在这里,我们将系列转换为strusingastype然后调用向量化str.isdigit
Also note that convert_objectsis deprecated and one should use to_numericfor the latest versions 0.17.0or newer
另请注意,convert_objects已弃用,to_numeric应用于最新版本0.17.0或更新版本
回答by Jeff
You can use convert_objects, which when convert_numeric=Truewill forcefully set all non-numeric to nan. Dropping them and indexing gets your result.
您可以使用convert_objects,它何时convert_numeric=True会强制将所有非数字设置为nan。删除它们并索引会得到你的结果。
This will be considerably faster that using applyon a larger frame as this is all implemented in cython.
这将比apply在更大的框架上使用快得多,因为这都是在 cython 中实现的。
In [30]: df[['A']].convert_objects(convert_numeric=True)
Out[30]:
A
0 1
1 2
2 NaN
3 3
4 NaN
In [31]: df[['A']].convert_objects(convert_numeric=True).dropna()
Out[31]:
A
0 1
1 2
3 3
In [32]: df[['A']].convert_objects(convert_numeric=True).dropna().index
Out[32]: Int64Index([0, 1, 3], dtype='int64')
In [33]: df.iloc[df[['A']].convert_objects(convert_numeric=True).dropna().index]
Out[33]:
A B
0 1 green
1 2 red
3 3 yellow
回答by Vidhya G
Note that convert_objectsis deprecated
请注意,convert_objects已弃用
>>> df[['A']].convert_objects(convert_numeric=True)
__main__:1: FutureWarning: convert_objects is deprecated. Use the data-type specific converters pd.to_datetime, pd.to_timedelta and pd.to_numeric.
From 0.17.0: use pd.to_numeric, set errors='coerce'so that incorrect parsing returns NaN. Use notnullto return a boolean mask to use on the original dataframe:
从 0.17.0 开始:使用pd.to_numeric,设置errors='coerce'以便不正确的解析返回 NaN。使用notnull一个布尔口罩返回使用原始数据帧:
>>> df[pd.to_numeric(df.A, errors='coerce').notnull()]
A B
0 1 green
1 2 red
3 3 yellow
回答by Lifu Huang
Personally, I think it will be more succinct to just use the built-in mapcompared with .apply()
就个人而言,我认为这将是更简洁,只需使用内置的map较.apply()
In [13]: df[map(pred, df['B'])]

