简体   繁体   中英

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.

This is how I would find those values (in a dataframe called df in a column called should_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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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