Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

python - Pandas: Filter to get rows which contain a character not on a specified list

Say this is my column (datatype is string)

'83.83'
'334-339.99'
'45'
'(34.23)'
'R34.2'

I would like to filter so that only rows which contain a character that is not on this list

['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'R', '.']

So I would only get these rows

'334-339.99'
'(34.23)'

I have been able to look up methods to specifically include or exclude certain characters and substrings in pandas

How to test if a string contains one of the substrings in a list, in pandas?

Which I don't think applies to my situation. If I filter for all the characters on the list, I get all the rows in my example. If I filter out for those characters, then I get no rows from my example.

question from:https://stackoverflow.com/questions/66055125/pandas-filter-to-get-rows-which-contain-a-character-not-on-a-specified-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Let us try

out = s[s.apply(lambda x : any([item not in r for item in x ]))]
Out[356]: 
1    334-339.99
3       (34.23)
dtype: object

Where r is the list of characters/substrings


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...