pandas dataframe select rows with ternary operator
18:22 10 Mar 2025

My requirement is simple, I need to select the rows from a Pandas DataFrame when one of two couple columns are populated or both. The attributes contain integer foreign keys. This works:

df_ofComment = df_obsFinding.loc[
    (None if df_obsFinding['Comments'] is None
     else df_obsFinding['Comments'].apply(len) != 0)]

gives me the rows from df_obsFindings where there is data in Comments. Good

This fails:

df_ofComment = df_obsFinding.loc[
    (None if df_obsFinding['Rejection_Comments'] is None 
     else df_obsFinding['Rejection_Comments'].apply(len) != 0)

Throws this error: TypeError: object of type 'NoneType' has no len()

I believe the data in 'Rejection_Comments' is dirtyer than 'Comments'

Under debug in the Comments col I see: [], [1234] , [1234], [ 456] etc.... Looks to me like lists and empty lists.

Under debug in Rejection_Comments I see None and Empty Boxes.
Silly me I thought checking for None would handle this.

In the end I was looking for a statement like this:

df_ofComment = df_obsFinding.loc[
    (None if df_obsFinding['Comments'] is None
     else df_obsFinding['Comments'].apply(len) != 0)
    |
    (None if df_obsFinding['Rejection_Comments'] is None
     else df_obsFinding['Rejection_Comments'].apply(len)!= 0)]

Maybe I am not going about this in a "Python" way

pandas dataframe conditional-operator