Bella
Bella

Reputation: 43

dataframe remove last digit from string if it is number

python dataframe

I want to delete the last character if it is number.

from current dataframe

data = {'d':['AAA2', 'BB 2', 'C', 'DDD ', 'EEEEEEE)', 'FFF ()', np.nan, '123456']}
df = pd.DataFrame(data)

to new dataframe

data = {'d':['AAA2', 'BB 2', 'C', 'DDD ', 'EEEEEEE)', 'FFF ()', np.nan, '123456'],
        'expected': ['AAA', 'BB', 'C', 'DDD', 'EEEEEEE)', 'FFF (', np.nan, '12345']}
df = pd.DataFrame(data)
df

ex

Upvotes: 0

Views: 378

Answers (1)

wasif
wasif

Reputation: 15518

Using .str.replace:

df['d'] = df['d'].str.replace(r'(\d)$','',regex=True)

Upvotes: 2

Related Questions