IIU C, попробуйте pandas.DataFrame.where
:
# Sample df
df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
'two'],
'bar': ['A', 'B', np.nan, 'A', 'B', 'C'],
'baz': [1, 2, np.nan, 4, 5, 6],
'zoo': ['x', 'y', 'z', 'q', 'w', 't']})
columns_to_process = ['bar', 'baz']
df[columns_to_process] = df[columns_to_process].where(df[columns_to_process].isna(), lambda x: x.astype(str)+'!')
df
Выход:
bar baz foo zoo
0 A!! 1.0!! one x
1 B!! 2.0!! one y
2 NaN NaN one z
3 A!! 4.0!! two q
4 B!! 5.0!! two w
5 C!! 6.0!! two t