Convert the '10yrs 5mon' categorical value to months

后端 未结 1 1864
猫巷女王i
猫巷女王i 2021-01-14 02:39

converting the column to numeric as pre-processing

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

Expected:

\'10yrs 1mo         


        
相关标签:
1条回答
  • 2021-01-14 02:49

    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
    
    0 讨论(0)
提交回复
热议问题