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部分时,它停止工作。
2条答案
按热度按时间nukf8bse1#
如果你使用
str.contains
,你需要在name中转义'('和')',因为它们是正则表达式中的特殊字符,如下所示:或者,您可以设置
regex=False
qc6wkl3g2#
当你搜索包含括号的文本时,Python会感到困惑。我不能运行你的代码,因为你正在导入你的dataframe,但我认为如果你这样做了:
会成功的