通过精确的单词匹配过滤pandas数据帧

时间:2016-10-06 07:13:12

标签: regex pandas

我在pandas中有以下数据框

          Date                             Title
58      March 2015        Data Visualization with JavaScript
63   December 2014          Eloquent JavaScript, 2nd Edition
90    October 2014             If Hemingway Wrote JavaScript
96   December 2014                       JavaScript for Kids
158  February 2014  Principles of Object-Oriented JavaScript
209  November 2005                          Wicked Cool Java

我必须过滤包含单词JavaScript的行。我正在做以下。

category_javascript = np.where(Publisher['Title'].str.contains(r'(?:\s|^)JavaScript(?:\s|$)'))

它让我跟随了

category_javascript
Out[106]: (array([ 58,  90,  96, 158], dtype=int64),)

它不会过滤63 December 2014 Eloquent JavaScript, 2nd Edition我认为因为JavaScript后面有逗号。无论标点符号还是组合,我都希望找到准确的单词。例如JavaScript-Book也可以。

请帮忙

1 个答案:

答案 0 :(得分:1)

IIUC你不需要正则表达式,只需要字符串public class PdfViewerControlRenderer : ViewRenderer<PdfViewerControl, UIView> { private readonly bool IsOniOS10; private UIViewController _controller; private QLPreviewController _qlPreviewController; public PdfViewerControlRenderer() { IsOniOS10 = UIDevice.CurrentDevice.CheckSystemVersion(10, 0); } protected override void OnElementChanged(ElementChangedEventArgs<PdfViewerControl> e) { if (e.NewElement != null) { _controller = new UIViewController(); _qlPreviewController = new QLPreviewController(); //... // Set QuickLook datasource here //... if (!IsOniOS10) { _controller.AddChildViewController(_qlPreviewController); _controller.View.AddSubview(_qlPreviewController.View); _qlPreviewController.DidMoveToParentViewController(_controller); } SetNativeControl(_controller.View); } } public override void LayoutSubviews() { base.LayoutSubviews(); _controller.View.Frame = Bounds; _controller.View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight; _qlPreviewController.View.Frame = Bounds; if (IsOniOS10) { _controller.View.AddSubview(_qlPreviewController.View); _qlPreviewController.DidMoveToParentViewController(_controller); } } }

JavaScript
category_javascript = np.where(Publisher['Title'].str.contains('JavaScript'))

您可以将变音符添加到正则表达式print (Publisher['Title'].str.contains('JavaScript')) 58 True 63 True 90 True 96 True 158 True 209 False Name: Title, dtype: bool print (Publisher[Publisher['Title'].str.contains('JavaScript')]) Date Title 58 March 2015 Data Visualization with JavaScript 63 December 2014 Eloquent JavaScript, 2nd Edition 90 October 2014 If Hemingway Wrote JavaScript 96 December 2014 JavaScript for Kids 158 February 2014 Principles of Object-Oriented JavaScript

[,;]