1

I have a pandas dataframe with a column/variable that consists entirely of text/strings.

I would like to create a new column that counts the number of times a keyword appears in the other column, for example:

keyword: 'the'

Column A: ['the dog', 'cat', 'the tree']

New Column: [1,0,1]

How do I do that?

0

1 Answer 1

0

You can use str.count:

df['new'] = df['col'].str.count(keyword)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, works perfectly!
BTW, I tried df['new'] = df['col'].count(keyword) before but it didn't work without the ".str". What does the ".str" do?
@AlexTheBoy - str mean in pandas text function, I guess for distinguish from function from pure python, because same names.
@AlexTheBoy - Also you can check working with text data

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.