pythonpandasisnumeric

Check for numeric value in text column - python


5 columns (col1 - col5) in a 10-column dataframe (df) should be either blank or have text values only. If any row in these 5 columns has an all numeric value, i need to trigger an error. Wrote the following code to identify rows where the value is all-numeric in 'col1'. (I will cycle through all 5 columns using the same code):

    df2 = df[df['col1'].str.isnumeric()]

I get the following error: ValueError: cannot mask with array containing NA / NaN values

This is triggered because the blank values create NaNs instead of False. I see this when I created a list instead using the following:

    lst = df['col1'].str.isnumeric()

Any suggestions on how to solve this? Thanks


Solution

  • Try this to work around the NaN

    import pandas as pd
    
    df = pd.DataFrame([{'col1':1}, {'col1': 'a'}, {'col1': None}])
    lst = df['col1'].astype(str).str.isnumeric()
    if lst.any():
        raise ValueError()