0

I'm learning python and currently trying to pull rows by specific keywords found in a column and save to a new csv file. Also, I'm trying to find a best way to get any possible hits for my keyword (ex: inc, Inc, INC, InC etc.):

My input.csv

id    name
1     ACME
2     Micro inc
3     Barber GmBH
4     Pizza Place

Desired output:

id name 2 Micro inc 3 Barber GmBH

My code so far:

import pandas as pd


df = pd.read_csv('/path/to/mynewtestfile.csv')


df[df['name'].str.contains("ball", "inc", "gmbh")]

df.to_csv('path/to/my/output.csv', index=False)

However, I received this error:

 TypeError: unsupported operand type(s) for &: 'str' and 'int'

Could someone help me with this? thanks in advance!

Baobab1988
  • 685
  • 13
  • 33
  • 2
    Use `df[df['name'].str.contains("ball|inc|gmbh")]` – jezrael Apr 01 '20 at 08:25
  • Hi Jezrael, and how can I avoid false positives like a word 'basketball' for example when looking for a keyword 'ball'? I think it would be then something else than str.contains I guess? I need only the actual keyword, but no matter if letters are capitalized or not. Thanks! – Baobab1988 Apr 01 '20 at 11:37
  • You can check last paragraph in [this](https://stackoverflow.com/a/47131550/2901002), need word boundaries. – jezrael Apr 01 '20 at 11:40

0 Answers0