1

I tried this inspired by the accepted answer here:

df = pd.DataFrame({'col1':['1', '']})
all_numeric = pd.to_numeric(df['col1'], errors='coerce').notnull().all().item()
print(all_numeric)

to detect that a columns is numeric (ignoring blanks + NULLs + NANs).

In the above code all_numeric is False (Python bool), which does not make sense or maybe it does? I thought I try to impute nan as the reason might be the empty value:

df = pd.DataFrame({'col1':['1', '']})
df = df.apply(lambda x: x.str.strip()).replace('', np.nan)
all_numeric = pd.to_numeric(df['col1'], errors='coerce').notnull().all().item()
print(all_numeric)

Same outcome. Maybe my way of checking if all values of a column are numeric (ignoring NULL/NAN/empty strings) is wrong? Thanks!

3
  • please add your data Commented Apr 1, 2022 at 15:41
  • @anon01 ??? is df = pd.DataFrame({'col1':['1', '']}) not data? Commented Apr 1, 2022 at 15:41
  • 1
    I missed that, aplolgies Commented Apr 1, 2022 at 15:42

2 Answers 2

0

You could strip white spaces and convert empty string to NaN, then drop it; then do the test:

out = pd.to_numeric(df['col1'].str.strip().replace('', pd.NA).dropna(), errors='coerce').notna().all().item()

Output:

True

This test throws False for the following input:

df = pd.DataFrame({'col1':['1', 's']})
Sign up to request clarification or add additional context in comments.

11 Comments

I am sorry I do not follow. Are you saying I should remove notnull()?
thanks. so how do I check if all values of a column are numeric (type object/string) ignoring empty entries, NULLs and NANs?
thanks but I want the logic for numeric detection ignore this and nulls and nans
no I do not - sorry
this is actually why I have df = df.apply(lambda x: x.str.strip()).replace('', np.nan)
|
-1

According to the official pandas doc, you can check a series of data if it's numeric or not.

>>df = pd.DataFrame({'col1':['1', '']})
>>df.col1.str.isnumeric()

0     True
1    False

5 Comments

df['col1'].str.isnumeric().mean() might work ...
Do you want to retrieve those cols which contain numeric values?
no I just want to transform them to numeric if there cardinality is also above threshold. long story
Can you add some sample output?
My example is pretty self explanatory I think?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.