Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exact Match
#1
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)]
Reply
#2
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
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 973 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Matching Exact String(s) Extra 4 1,860 Jan-12-2022, 04:06 PM
Last Post: Extra
  replace exact word marfer 1 6,013 Oct-11-2021, 07:08 PM
Last Post: snippsat
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,167 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Two exact "Fors" giving me different results? FilGonca 2 1,957 May-04-2020, 12:36 PM
Last Post: FilGonca
  Creating new list based on exact regex match in original list interjectdirector 1 2,254 Mar-08-2020, 09:30 PM
Last Post: deanhystad
  Confusion in exact term used for ? ift38375 2 2,527 Jul-22-2019, 08:58 AM
Last Post: buran
  Finding exact phrase in list graham23s 2 2,836 Mar-13-2019, 06:47 PM
Last Post: graham23s
  Appending inside elements of lists, in an exact location nhan826 1 2,003 Oct-23-2018, 01:17 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

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