pandas 如何检查熊猫数据框是否仅包含数字列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54426845/
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 check if a pandas dataframe contains only numeric column wise?
提问by Raja Sahe S
I want to check every column in a dataframe whether it contains only numeric. How can i find it.
我想检查数据框中的每一列是否只包含数字。我怎样才能找到它。
回答by rafaelc
You can check that using to_numeric
and coercing errors:
您可以检查使用to_numeric
和强制错误:
pd.to_numeric(df['column'], errors='coerce').notnull().all()
For all columns, you can iterate through columns or just use apply
对于所有列,您可以遍历列或仅使用 apply
df.apply(lambda s: pd.to_numeric(s, errors='coerce').notnull().all())
E.g.
例如
df = pd.DataFrame({'col' : [1,2, 10, np.nan, 'a'],
'col2': ['a', 10, 30, 40 ,50],
'col3': [1,2,3,4,5.0]})
Outputs
输出
col False
col2 False
col3 True
dtype: bool
回答by Karn Kumar
You can draw a True / False comparison using isnumeric()
您可以使用以下方法绘制真/假比较 isnumeric()
Example:
例子:
>>> df
A B
0 1 1
1 NaN 6
2 NaN NaN
3 2 2
4 NaN NaN
5 4 4
6 some some
7 value other
Results:
结果:
>>> df.A.str.isnumeric()
0 True
1 NaN
2 NaN
3 True
4 NaN
5 True
6 False
7 False
Name: A, dtype: object
# df.B.str.isnumeric()
with apply()
method which seems more robust in case you need corner to corner comparison:
apply()
如果您需要角到角比较,方法似乎更健壮:
DataFrame having two different columns one with mixed type another with numbers only for test:
DataFrame 有两个不同的列,一个是混合类型,另一个是数字,仅用于测试:
>>> df
A B
0 1 1
1 NaN 6
2 NaN 33
3 2 2
4 NaN 22
5 4 4
6 some 66
7 value 11
Result:
结果:
>>> df.apply(lambda x: x.str.isnumeric())
A B
0 True True
1 NaN True
2 NaN True
3 True True
4 NaN True
5 True True
6 False True
7 False True
回答by TYZ
Let's say you have a dataframe called df
, if you do:
假设您有一个名为 的数据框df
,如果您这样做:
df.select_dtypes(include=["float", 'int'])
This will return all the numeric columns, you can check if this is the same as the original df
.
这将返回所有数字列,您可以检查这是否与原始df
.
Otherwise, you can also use the exclude
parameter:
否则,您也可以使用exclude
参数:
df.select_dtypes(exclude=["float", 'int'])
and check if this gives you an empty dataframe.
并检查这是否为您提供了一个空的数据框。
回答by Vaishali
This will return True if all columns are numeric, False otherwise.
如果所有列都是数字,这将返回 True,否则返回 False。
df.shape[1] == df.select_dtypes(include=np.number).shape[1]
To select numeric columns:
要选择数字列:
new_df = df.select_dtypes(include=np.number)