问题
In pandas.DataFrame.filter is there a way to use the parameters "like" or "regex" so they support an OR condition. for example:
df.filter(like='bbi', axis=1)
would filter on columns with bbi
in their name, but how would I filter on columns containing 'bbi'
OR 'abc'
?
A few options that fail:
df.filter(like='bbi' or 'abc', axis=1)
df.filter(like=('bbi' or 'abc'), axis=1)
回答1:
I would do the below:
Setup:
df=pd.DataFrame(np.random.randint(0,20,20).reshape(5,4),
columns=['abcd','bcde','efgh','bbia'])
print(df)
abcd bcde efgh bbia
0 10 17 2 7
1 7 12 18 9
2 17 7 11 17
3 14 4 2 9
4 15 10 12 11
Solution:
Using df.filter
:
df.filter(regex=r'(abc|bbi)')
abcd bbia
0 10 7
1 7 9
2 17 17
3 14 9
4 15 11
回答2:
Not familiar with the filter
command. But you could achieve what you want like this I think:
df[(df['column'].str.contains('bbi', case=False)) | (df['column'].str.contains('abc', case=False))]
回答3:
Please find the attached screenshot.
Regex search is slower. So we keep regex=False
.
Hope this helps.Thank you.
来源:https://stackoverflow.com/questions/58752330/filter-dataframe-index-on-multiple-conditions