【问题标题】:UserWarning: This pattern has match groups用户警告:此模式具有匹配组
【发布时间】:2018-12-06 21:38:42
【问题描述】:
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。要实际获取组,请使用 str.extract。 iFlow = Sales.loc[Sales['Product'].str.contains('Vector HF/LF (Opt 2)', na=False), "18-Jun"] 系列([],名称:18-Jun,dtype:对象) C:\Users\khalha\eclipse-workspace\hariskk\hey\hello.py:12: UserWarning: This pattern has match groups。要实际获取组,请使用 str.extract。 iFlowmay = MaySales.loc[MaySales['Product'].str.contains('Vector HF/LF (Opt 2)', na=False), "18-Jun"] 系列([],名称:18-Jun,dtype:对象)

此代码适用于第一个块,但当我添加 Maysales 部分时,它停止工作。

【问题讨论】:

标签: python pandas


【解决方案1】:

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

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

会有用的

【讨论】:

    猜你喜欢
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 2016-05-21
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    相关资源
    最近更新 更多