Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...