I have a dataframe with a column of lists, these consists of empty list as well and list with values which includes ‘nan’. I need to remove the nan and create another column with total count of values within list in each cell.
data = {'Value_count': [0, 2, 2, 2],
'Risk Rating': [[], ['No Risk', np.nan], ['Medium', 'No Risk/Not Rated'], ['High', 'NaN']]}
I have tried the below code first to remove the nans
def remove_nan_from_list(lst):
return [item for item in lst if not pd.isna(item)]
df['Risk Rating'] = df['ListColumn'].apply(remove_nan_from_list)
but its coming up list of list of strings like [[‘N’, ‘o’, ”, ‘R’, ‘i’, ‘s’, ‘k’]]
Apologies for not providing detailed result as this is done for official work
You should use a nested list comprehension:
[[x for x in lst if not pd.isna(x)] for lst in df['Risk Rating']]