1
data = {
'list_id' : [[50, None],[20, 68],[10, 7],[73, 4, 26, 3],[50, None],[68, 20, 61, 62],[68, None]]
}

df = pd.DataFrame.from_dict(data)
print (df)

I tried the below steps,

expected Output

    data = {
'list_id' : [[50],[20, 68],[10, 7],[73, 4, 26, 3],[50],[68, 20, 61, 62],[68]]
}
df = pd.DataFrame.from_dict(data)
print (df)
            list_id
0              [50]
1          [20, 68]
2           [10, 7]
3    [73, 4, 26, 3]
4              [50]
5  [68, 20, 61, 62]
6              [68]



 df[['list_id']] = df['list_id'].apply(lambda el: [  f'' if x is None else x for x in el])
 df[['list_id']] = df['list_id'].apply(lambda el: [  f'' if x is None for x in el])

Need to replace None values in column with list elements, either as an empty '' string or None being removed, not sure about np.nan..

1 Answer 1

2

For remove None or NaNs values use notna in list comprehension:

df['list_id'] = df['list_id'].apply(lambda el: [x for x in el if pd.notna(x)])
print (df)
            list_id
0              [50]
1          [20, 68]
2           [10, 7]
3    [73, 4, 26, 3]
4              [50]
5  [68, 20, 61, 62]
6              [68]

For remove only Nones compare by None with not:

df['list_id'] = df['list_id'].apply(lambda el: [x for x in el if x is not None])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.