4

converting the column to numeric as pre-processing

Aging
'10yrs 1mon'
'9yrs 8mon'
'25yrs 5mon'

Expected:

'10yrs 1mon'     121
'9yrs 8mon'      116
'25yrs 5mon'     305
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Vinoth96
  • 353
  • 1
  • 2
  • 6
  • maybe this could help: https://dateparser.readthedocs.io/ . similiar question asked before: https://stackoverflow.com/questions/9775743/how-can-i-parse-free-text-time-intervals-in-python-ranging-from-years-to-second – hiro protagonist Apr 20 '19 at 07:43
  • The solution given below by jezrael is better. Similar/same question: https://stackoverflow.com/questions/55765498/attributeerror-can-only-use-dt-accessor-with-datetimelike-values-in-0yrs-0mon/55765835#55765835 – Sid Apr 20 '19 at 10:47

1 Answers1

8

Use Series.str.extract with casting to integers to new DataFrame and add new column by multiple 12 first and add second column:

import pandas as pd

df1 = df['Aging'].str.extract('(\d+)yrs\s+(\d+)mon').astype(int)
df['new'] = df1[0] * 12 + df1[1]
print (df)
          Aging  new
0  '10yrs 1mon'  121
1   '9yrs 8mon'  116
2  '25yrs 5mon'  305
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252