RAJ KUMAR NAYAK
RAJ KUMAR NAYAK

Reputation: 21

Check a given word in each cell and extract it to put in an array

I have a multiple column and in one of a column There is a paragraph written along with a keyword. I need to extract that keyword and put it in an array.

EX: Example

Now I have to go through every row of column 3 and find Keyword "TEST" and extract after : word. Like ENGLISH, MATH, PSYCHLOGY etc into an array. Further I'll create a text file and make a sentance using these extracted words. I am not able to find exact logic to extract these words.

Upvotes: 0

Views: 75

Answers (2)

keramat
keramat

Reputation: 4543

Use str.extract as follows:

temp = df['column3'].str.extract('TEST:(.*)')

For the second part if I understand you well:

temp.to_string(index=False)

Upvotes: 1

Kang San Lee
Kang San Lee

Reputation: 382

You can use pandas.Series.str.extract.

import pandas as pd

df = pd.DataFrame({
    'col1': ['a', 'b', 'c'], 
    'col2': ['a', 'b', 'c'], 
    'col3': ['a\n\nTEST: MATH', 'b\nTEST: ENG', 'c\n\nTEST: PSY']
})

df['col3'].str.extract('TEST:(.*)$')

Upvotes: 1

Related Questions