I have the following dataframe:
>>> df = pd.DataFrame(['0123_GRP_LE_BNS', 'ABC_GRP_BNS', 'DEF_GRP', '456A_GRP_SSA'], columns=['P'])
>>> df
P
0 0123_GRP_LE_BNS
1 ABC_GRP_BNS
2 DEF_GRP
3 456A_GRP_SSA
and want to remove characters appear after GRP if they are not '_LE', or remove characters after GRP_LE.
The desired output is:
0 0123_GRP_LE
1 ABC_GRP
2 DEF_GRP
3 456A_GRP
I used the following pattern matching. the ouput was not expected:
>>> df['P'].replace({r'(.*_GRP)[^_LE].*':r'\1', r'(.*GRP_LE)_.*':r'\1'}, regex=True)
0 0123_GRP_LE
1 ABC_GRP_BNS
2 DEF_GRP
3 456A_GRP_SSA
Name: P, dtype: object
Why the negation in r'(.*_GRP)[^_LE].*' does not work?