Home > Back-end >  Check a given word in each cell and extract it to put in an array
Check a given word in each cell and extract it to put in an array

Time:04-22

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.

CodePudding user response:

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:(.*)$')

CodePudding user response:

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)
  • Related