PANDAS Finding the exact word and before word in a column of string and append that new column in python (pandas) column

后端 未结 2 1028
梦谈多话
梦谈多话 2020-12-19 16:35

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          


        
2条回答
  •  时光说笑
    2020-12-19 17:15

    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]
    

提交回复
热议问题