Find the target word and the before word in col_a and append matched string in col_b_PY and col_c_LG columns
This code i have tried to
Check with
df['col_c_LG'],df['col_c_PY']=df['col_a'].str.extract(r"(\w+\s+LG)"),df['col_a'].str.extract(r"(\w+\s+PY)")
df
Out[474]:
col_a ... col_c_PY
0 Python PY is a general-purpose language LG ... Python PY
1 Programming language LG in Python PY ... Python PY
2 Its easier LG to understand PY ... understand PY
3 The syntax of the language LG is clean PY ... clean PY
[4 rows x 3 columns]