Python Forum
Pandas's regular expression function result is so strange
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas's regular expression function result is so strange
#7
sorry for another question.
I tried to search lots of data from Excel. After importing data to list(data structure).
I tried two methods.
1. using list with re module search.
2. Transfer list --> dataframe and then apply with .str.contains() method
Both of them can be workable. But dataframe is more slower than pandas dataframe. Is it reasonable?
PS: python console shows below user warning
UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
  return func(self, *args, **kwargs)
(Jun-12-2020, 10:14 AM)snippsat Wrote:
(Jun-12-2020, 09:35 AM)cools0607 Wrote: I wonder if .str.contains includes specified functions just like re module?
Yes str.contains can take regular expression patterns as in the re module.
Quote:For example: '^AA' expresses only searching words start with AA.
Yes that would work,Pandas have a lot build in so there is also a str.startswith.
If wonder if something works,then is best to do a test.
import pandas as pd

d = {
    'Quarters' : ['quarter1','quarter2','quarter3','quarter4'],
     'Description': ['AA year', 'BB year', 'CC year', 'AA year'],
     'Revenue': [23.5, 54.6, 5.45, 41.87]
}
df = pd.DataFrame(d)
Test usage:
>>> df[df['Description'].str.contains(r'^AA')]
  Description  Quarters  Revenue
0     AA year  quarter1    23.50
3     AA year  quarter4    41.87
>>> df[df['Description'].str.contains(r'^AA|BB')]
  Description  Quarters  Revenue
0     AA year  quarter1    23.50
1     BB year  quarter2    54.60
3     AA year  quarter4    41.87

>>> # Using str.startswith
>>> df[df['Description'].str.startswith('AA')]
  Description  Quarters  Revenue
0     AA year  quarter1    23.50
3     AA year  quarter4    41.87
>>> df[df['Description'].str.startswith(('AA', 'BB'))]
  Description  Quarters  Revenue
0     AA year  quarter1    23.50
1     BB year  quarter2    54.60
3     AA year  quarter4    41.87 
Reply


Messages In This Thread
RE: Pandas's regular expression function result is so strange - by cools0607 - Jun-15-2020, 07:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Regular expression help anilrajr 4 355 May-08-2024, 06:18 PM
Last Post: deanhystad
  strange result in xor zapad 6 350 May-07-2024, 09:09 PM
Last Post: deanhystad
  data validation with specific regular expression shaheen07 0 383 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Regular Expression search to comment lines of code Gman2233 5 1,747 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,737 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Regex Expression With Code Query In Pandas eddywinch82 8 2,441 Apr-13-2022, 09:12 AM
Last Post: snippsat
  Need help with my code (regular expression) shailc 5 2,010 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,218 Mar-09-2022, 10:34 PM
Last Post: snippsat
  Use of groupby in a function with Pandas Paulman 0 983 Dec-03-2021, 04:56 PM
Last Post: Paulman
  regular expression question Skaperen 4 2,579 Aug-23-2021, 06:01 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020