Pedro Alves :
I have a dataframe that have two values:
df = pd.DataFrame({'Col1': ['Table_A112', 'Table_A_112']})
What I am trying to do is to remove the numeric digits in case of the split('_') only have numeric digits. The desired output is:
Table_A112
Table_A_
For that I am using the following code:
import pandas as pd
import difflib
from tabulate import tabulate
import string
df = pd.DataFrame({'Col1': ['Table_A112', 'Table_A_112']})
print(tabulate(df, headers='keys', tablefmt='psql'))
df['Col2'] = df['Col1'].str.rstrip(string.digits)
print(tabulate(df, headers='keys', tablefmt='psql'))
But it gives me the following output:
Table_A
Table_A_
How can do what I want?
Thanks!
yatu :
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