Python Unsupported operator type for & error handling

I’m trying to do an if statement using rows of a dataframe. Sometimes, the value can be None, and this is what I am wanting to check for.

for row in tqdm(df.index):

        add = df.loc[row, "add"]
        name = df.loc[row, "name"]

        if str(add) is not None and str(name) is not None:
            add_name = add + " " + name
        elif str(add) is not None:
            add_name = add
        elif str(name) is not None:
            add_name = name
        add_names.append(add_name)

As you can see above, if neither of the values are None, then I want to concatenate. If either is None, then I want to use the other value.

However, I keep getting this error:

TypeError: unsupported operand type(s) for &: 'NoneType' and 'str'

I can’t figure out how to solve this. I thought by trying to cast to a str this would work but sadly not.

EDIT: This is a sample DF I am testing with:

df = pd.DataFrame(
        {
            "name": [
                "Random Name",
                "Another Name"
            ],
            "add": [
                None,
                "Pre text"
            ]
        }
    )

I am expecting the new field to be:

Random Name
Pre text Another Name

Answer

Sample:

df = pd.DataFrame(
        {
            "name": [
                "Random Name",
                "Another Name",
                None,
            ],
            "add": [
                None,
                "Pre text",
                'something'
            ]
        }
    )
    

First replace missing values use Series.fillna, join by + and last add Series.str.strip for remove traling whitespaces :

df['add_name'] = (df['add'].fillna('') + ' ' + df['name'].fillna('')).str.strip()

print (df)
           name        add               add_name
0   Random Name       None            Random Name
1  Another Name   Pre text  Pre text Another Name
2          None  something              something

Another solution in numpy.where with Series.fillna by empty string if match both masks and join by space or replace missing values by another column:

df['add_name'] = np.where(df['add'].notna() & df['name'].notna(), 
                          df['add'].fillna('') + ' ' + df['name'].fillna(''),
                          df['add'].fillna(df['name']))

print (df)
           name        add               add_name
0   Random Name       None            Random Name
1  Another Name   Pre text  Pre text Another Name
2          None  something              something

If need test more masks, e.g. also misisng values (by invert mask by ~) is possible use numpy.select:

df = pd.DataFrame(
        {
            "name": [
                "Random Name",
                "Another Name",
                None,
                None
            ],
            "add": [
                None,
                "Pre text",
                'something',
                None
            ]
        }
    )

m1 = df['add'].notna()
m2 = df['name'].notna()
    
s1 = df['add'].fillna('') + ' ' + df['name'].fillna('')
s2 = df['add'].fillna(df['name'])

df['add_name'] = np.select([m1 & m2,
                            m1 & ~m2,
                            ~m1 & m2], 
                           [s1, df['add'], df['name']], default=None)

print (df)
           name        add               add_name
0   Random Name       None            Random Name
1  Another Name   Pre text  Pre text Another Name
2          None  something              something
3          None       None                   None