Вы можете использовать if
в заданном понимании, если в NaN
s отсутствуют значения:
df = pd.DataFrame({'column_1': [1, 1],
'column_2': [[np.nan, '18m'], ['lk', 'r']],
'column_3': [['kjaf'], ['ddd']]})
print (df)
column_1 column_2 column_3
0 1 [nan, 18m] [kjaf]
1 1 [lk, r] [ddd]
cols = ['column_2', 'column_3']
df[cols] = df[cols].applymap(lambda x: set([i for i in x if pd.notna(i)]))
#oldier pandas versions
#df[cols] = df[cols].applymap(lambda x: set([i for i in x if pd.notnull(i)]))
print (df)
column_1 column_2 column_3
0 1 {18m} {kjaf}
1 1 {r, lk} {ddd}
Если NaN
s - строки:
df = pd.DataFrame({'column_1': [1, 1],
'column_2': [['NaN', '18m'], ['lk', 'r']],
'column_3': [['kjaf'], ['ddd']]})
print (df)
column_1 column_2 column_3
0 1 [NaN, 18m] [kjaf]
1 1 [lk, r] [ddd]
cols = ['column_2', 'column_3']
df[cols] = df[cols].applymap(lambda x: set([i for i in x if i != 'NaN']))
print (df)
column_1 column_2 column_3
0 1 {18m} {kjaf}
1 1 {r, lk} {ddd}