pandas 用户警告:此模式具有匹配组

bprjcwpo  于 2023-04-28  发布在  其他
关注(0)|答案(2)|浏览(95)
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)

我得到错误消息:

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)

这段代码处理第一个块,但是当我添加Maysales部分时,它停止工作。

nukf8bse

nukf8bse1#

如果你使用str.contains,你需要在name中转义'('和')',因为它们是正则表达式中的特殊字符,如下所示:

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

或者,您可以设置regex=False

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

qc6wkl3g2#

当你搜索包含括号的文本时,Python会感到困惑。我不能运行你的代码,因为你正在导入你的dataframe,但我认为如果你这样做了:

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

会成功的

相关问题