Python - Pandas - Remove only splits that only numeric but maintain if it have alphabetic
You can do something like:
s = df['Col1'].str.split('_',expand=True).stack()
s.mask(s.str.isdigit(), '').groupby(level=0).agg('_'.join)
Output:
0 Table_A112
1 Table_A_
dtype: object
Here's one way using str.replace
:
df = pd.DataFrame({'Col1': ['Table_A112', 'Table_A_112', 'Table_112_avs']})
print(df)
Col1
0 Table_A112
1 Table_A_112
2 Table_112_avs
df.Col1.str.replace(r'(?:^|_)(\d+)(?:$|_)', '_', regex=True)
0 Table_A112
1 Table_A_
2 Table_avs
Name: Col1, dtype: object
See demo