Reputation: 21
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.
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
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
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