0
import csv
import os
import pandas as pd
os.chdir('C:\\Users\\khalha\\Desktop\\RealExcel')
filename = 'sales.csv'

Sales = pd.read_csv('sales.csv')
iFlow = Sales.loc[Sales['Product'].str.contains('Vector HF/LF (Opt 2)', 
na=False), "18-Jun"]
print(iFlow)

MaySales = pd.read_csv('maysales.csv')
iFlowmay = MaySales.loc[MaySales['Product'].str.contains('Vector HF/LF (Opt 
2)', na=False), "18-Jun"]
print(iFlowmay)

I get the error message:

C:\Users\khalha\eclipse-workspace\hariskk\hey\hello.py:8: UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
  iFlow = Sales.loc[Sales['Product'].str.contains('Vector HF/LF (Opt 2)', na=False), "18-Jun"]
Series([], Name: 18-Jun, dtype: object)
C:\Users\khalha\eclipse-workspace\hariskk\hey\hello.py:12: UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
  iFlowmay = MaySales.loc[MaySales['Product'].str.contains('Vector HF/LF (Opt 2)', na=False), "18-Jun"]
Series([], Name: 18-Jun, dtype: object)

This code works with the first block, but when I add the Maysales portion, it stops working.

Michael Norman
  • 121
  • 2
  • 5
  • 13
  • Does this answer your question? [Python: UserWarning: This pattern has match groups. To actually get the groups, use str.extract](https://stackoverflow.com/questions/39901550/python-userwarning-this-pattern-has-match-groups-to-actually-get-the-groups) – tyersome Dec 18 '21 at 23:47

2 Answers2

0

If you use str.contains, you need to escape '(' and ')' in name because they are special chars in regex as follows:

iFlowmay = MaySales.loc[MaySales['Product'].str.contains('Vector HF/LF \(Opt 
2\)', na=False), "18-Jun"]

Alternatively, you can set regex=False

iFlowmay = MaySales.loc[MaySales['Product'].str.contains('Vector HF/LF (Opt 
2)', na=False, regex=False), "18-Jun"]
-1

Python gets confused when you search for text that includes parenthesis. I can't just run your code since you are importing your dataframe, but I think if you just did:

iFlow = Sales.loc[Sales['Product'].str.contains('\Vector HF/LF (Opt 2', na=False), "18-Jun"]

it will work

Tom
  • 1,003
  • 2
  • 13
  • 25
  • I get a "error: missing ), unterminated subpattern..." when dropping the closing parenthesis. Adding the \ before the string also throws an error. However, dropping the parentheses all together fixed my related issue. Thank you! – Wilson Sauthoff Apr 24 '23 at 23:15