Search code examples
pandasdataframenonetype

TypeError: 'NoneType' object is not subscriptable when checking for nonetype


I am trying to detect Nonetype in a single cell of a 1 column, 15 row dataframe with the following:

if str(row.iloc[13][:]) is None:
     print("YES")

But this causes the error: TypeError: 'NoneType' object is not subscriptable


Solution

  • If row is Series, then if select value by position:

    row.iloc[13]
    

    output is scalar. So cannot slice scalar value by [:]. Also if convert to string by str cannot compare by None, but by string like:

    if str(row.iloc[13]) == 'None':
    

    If want compare by None:

    if row.iloc[13] is None:
    

    Or if compare by NaN or None:

    if pd.isna(row.iloc[13]):