Home > database >  Delete characters on dataframe python
Delete characters on dataframe python

Time:09-27

ID Duration 
0   6-month
1   12-month
2   24-month

I want to delete '-month'. Can you help me ?

Thanks

CodePudding user response:

Assuming that month number comes before the '-month' in duration

You can use regex to extract the digits.

df['Duration']=df['Duration'].str.extract(r'(\d*)')
df
    ID  Duration
0    0       6
1    1      12
2    2      24

CodePudding user response:

Here's another solution

df['Duration']= df['Duration'].apply(lambda x:x.replace('-month',''))
  • Related