Python Forum

Full Version: Exact Match
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

The first line of code is supposed to find string A180, and shift down 9 rows and grab that value. My problem is, is that I also have A180X inside this file, so It's not grabbing the correct information cause its also finding A180X.

The second Line I figured if I used ==String it would find the exact match and it does. My problem is, is when I add shift(9) I get an error 'str' object has no attribute 'shift'. Does anyone know how I can find the exact match but still use shift?

N2=NewFile2[NewFile2['FindString'].str.match(String,na=False).shift(9).fillna(False)]
   
N3=NewFile2[NewFile2['FindString']==(String).shift(9).fillna(False)]
Should tell that you use Pandas not all is familiar with the syntax,and if give a working code example is easier to help.
In regex can use \b for a exact word match.
Bye working code i mean this.
import pandas as pd

d = {
    'Quarters' : ['quarter1','quarter2','quarter3','quarter4'],
     'Description': ['A180', ' A180X', ' A180123', 'A180'],
     'Revenue': [23.5, 54.6, 5.45, 41.87]
}
df = pd.DataFrame(d)
Usage.
>>> df['Description'].str.match(r'\bA180\b')
0     True
1    False
2    False
3     True
Name: Description, dtype: bool
>>> 
>>> df = df[df['Description'].str.match(r'\bA180\b')]
>>> df
   Quarters Description  Revenue
0  quarter1        A180    23.50
3  quarter4        A180    41.87
>>> 
>>> df.shift(-1)
   Quarters Description  Revenue
0  quarter4        A180    41.87
3       NaN         NaN      NaN
(Jul-24-2020, 11:48 PM)snippsat Wrote: [ -> ]Should tell that you use Pandas not all is familiar with the syntax,and if give a working code example is easier to help.
In regex can use \b for a exact word match.
Bye working code i mean this.
import pandas as pd

d = {
    'Quarters' : ['quarter1','quarter2','quarter3','quarter4'],
     'Description': ['A180', ' A180X', ' A180123', 'A180'],
     'Revenue': [23.5, 54.6, 5.45, 41.87]
}
df = pd.DataFrame(d)
Usage.
>>> df['Description'].str.match(r'\bA180\b')
0     True
1    False
2    False
3     True
Name: Description, dtype: bool
>>> 
>>> df = df[df['Description'].str.match(r'\bA180\b')]
>>> df
   Quarters Description  Revenue
0  quarter1        A180    23.50
3  quarter4        A180    41.87
>>> 
>>> df.shift(-1)
   Quarters Description  Revenue
0  quarter4        A180    41.87
3       NaN         NaN      NaN

Hello,

using (r'\bA180\b') only works if you put the actual word between \b and \b, I wouldn't be able to use that because my string is constantly changing throughout the loop. That's why I have .str.match(String). It might be A180 for the first loop, and then second loop around it might me A180X, next A170.

(Jul-26-2020, 03:29 PM)Kristenl2784 Wrote: [ -> ]
(Jul-24-2020, 11:48 PM)snippsat Wrote: [ -> ]Should tell that you use Pandas not all is familiar with the syntax,and if give a working code example is easier to help.
In regex can use \b for a exact word match.
Bye working code i mean this.
import pandas as pd

d = {
    'Quarters' : ['quarter1','quarter2','quarter3','quarter4'],
     'Description': ['A180', ' A180X', ' A180123', 'A180'],
     'Revenue': [23.5, 54.6, 5.45, 41.87]
}
df = pd.DataFrame(d)
Usage.
>>> df['Description'].str.match(r'\bA180\b')
0     True
1    False
2    False
3     True
Name: Description, dtype: bool
>>> 
>>> df = df[df['Description'].str.match(r'\bA180\b')]
>>> df
   Quarters Description  Revenue
0  quarter1        A180    23.50
3  quarter4        A180    41.87
>>> 
>>> df.shift(-1)
   Quarters Description  Revenue
0  quarter4        A180    41.87
3       NaN         NaN      NaN

Hello,

using (r'\bA180\b') only works if you put the actual word between \b and \b, I wouldn't be able to use that because my string is constantly changing throughout the loop. That's why I have .str.match(String). It might be A180 for the first loop, and then second loop around it might me A180X, next A170. I have to many files to loop through to try and define each string individually.