steadynappin
steadynappin

Reputation: 497

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?

Upvotes: 1

Views: 1015

Answers (1)

René
René

Reputation: 4827

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]]

Input:

  should_be_numbers
0                 1
1                22
2                 A
3                BB
4           [1, 22]
5           [A, BB]
6            A1BB22
7               NaN
8              3.13

Output:

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

Upvotes: 2

Related Questions