Reputation: 554
I have a df that contains list as elements.
df = pd.DataFrame({'q': [['', 'Id'], [', ', 'Be', ', '], [', ', ''], ['ProgramServiceStatusId', '']]})
I want to only keep the elements which contains alphanumeric characters.
the expected output would be
q
0 [Id]
1 [Be]
2 []
3 [ProgramServiceStatusId]
I referred this question, but it requires two loops:
Python keep only alphanumeric words from list
First one to iterate over df column and second one to iterate over individual list.
I wanted to know if there is an easier way to do this.
Thanks
Upvotes: 1
Views: 587
Reputation: 862641
Use lambda function with Series.apply
:
df['q'] = df['q'].apply(lambda x: [i for i in x if i.isalpha()])
print (df)
q
0 [Id]
1 [Be]
2 []
3 [ProgramServiceStatusId]
Upvotes: 1