简体   繁体   English

在 pandas dataframe 中查找非数字值

[英]find non-numeric values in a pandas dataframe

Say I import a csv into pandas, and I realize there are some non-numeric values in a column that I expect to be all numeric.假设我将 csv 导入 pandas,我意识到列中有一些非数字值,我希望它们都是数字。

This is how I would find those values (in a dataframe called df in a column called should_be_numbers ):这就是我找到这些值的方式(在名为 should_be_numbers 的列中名为dfshould_be_numbers中):

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

My question: Is there a cleaner/more pythonic/less clunky way to do this?我的问题:有没有更清洁/更蟒蛇/不那么笨重的方式来做到这一点?

df = pd.DataFrame({'should_be_numbers': [1, 22, 'A', 'BB', [1, 22], ['A', 'BB'], 'A1BB22', np.nan, 3.13]})
df[[not (isinstance(value, int) or isinstance(value, float)) for value in df.should_be_numbers]]

Output: Output:

  should_be_numbers
2                 A
3                BB
4           [1, 22]
5           [A, BB]
6            A1BB22

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM