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
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