👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I would like to cleanly filter a dataframe using regex on one of the columns.
For a contrived example:
In [210]: foo = pd.DataFrame({"a" : [1,2,3,4], "b" : ["hi", "foo", "fat", "cat"]})
In [211]: foo
Out[211]:
a b
0 1 hi
1 2 foo
2 3 fat
3 4 cat
I want to filter the rows to those that start with f
using a regex. First go:
In [213]: foo.b.str.match("f.*")
Out[213]:
0 []
1 ()
2 ()
3 []
That"s not too terribly useful. However this will get me my boolean index:
In [226]: foo.b.str.match("(f.*)").str.len() > 0
Out[226]:
0 False
1 True
2 True
3 False
Name: b
So I could then do my restriction by:
In [229]: foo[foo.b.str.match("(f.*)").str.len() > 0]
Out[229]:
a b
1 2 foo
2 3 fat
That makes me artificially put a group into the regex though, and seems like maybe not the clean way to go. Is there a better way to do this?
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from How to filter rows in pandas by regex, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How to filter rows in pandas by regex
- Deutsch How to filter rows in pandas by regex
- Français How to filter rows in pandas by regex
- Español How to filter rows in pandas by regex
- Türk How to filter rows in pandas by regex
- Русский How to filter rows in pandas by regex
- Português How to filter rows in pandas by regex
- Polski How to filter rows in pandas by regex
- Nederlandse How to filter rows in pandas by regex
- 中文 How to filter rows in pandas by regex
- 한국어 How to filter rows in pandas by regex
- 日本語 How to filter rows in pandas by regex
- हिन्दी How to filter rows in pandas by regex
Munchen | 2023-03-24
I was preparing for my coding interview, thanks for clarifying this - How to filter rows in pandas by regex in Python is not the simplest one. Will use it in my bachelor thesis
California | 2023-03-24
Ev PHP module is always a bit confusing 😭 How to filter rows in pandas by regex is not the only problem I encountered. Will use it in my bachelor thesis
Moscow | 2023-03-24
Thanks for explaining! I was stuck with How to filter rows in pandas by regex for some hours, finally got it done 🤗. Checked yesterday, it works!