Home > Back-end >  converting a "2 yrs 2 mon" string to total months
converting a "2 yrs 2 mon" string to total months

Time:04-21

How can this [DataFrame] data be converted as months, ex: 2 yrs 2 mon as 26

0     0yrs 0mon
1     2yrs 6mon
2     0yrs 0mon
3     0yrs 0mon
4     3yrs 1mon
5    15yrs 2mon
6    1yrs 10mon
7     0yrs 0mon
8     0yrs 0mon
9    3yrs 10mon
Name: CREDIT.HISTORY.LENGTH, dtype: object

CodePudding user response:

Use Series.str.extract for numbers before yrs and mon, convert to integers and then create new column:

y = data["CREDIT.HISTORY.LENGTH"].str.extract("(\d )yrs", expand=False).astype(int)
m = data["CREDIT.HISTORY.LENGTH"].str.extract("(\d )mon", expand=False).astype(int)
data["MONTHS"] = y * 12   m
    
print (data)
  CREDIT.HISTORY.LENGTH  MONTHS
0             0yrs 0mon       0
1             2yrs 6mon      30
2             0yrs 0mon       0
3             0yrs 0mon       0
4             3yrs 1mon      37
5            15yrs 2mon     182
6            1yrs 10mon      22
7             0yrs 0mon       0
8             0yrs 0mon       0
9            3yrs 10mon      46

CodePudding user response:

You can achieve this with a single extract command taking advantage of multiplication by a Series:

df['MONTHS'] = (df['CREDIT.HISTORY.LENGTH']
 .str.extract(r'(\d )yrs\s*(\d )mon').astype(int)
 .mul(pd.Series({0: 12, 1: 1})) # multiply years by 12, leave months unchanged
 .sum(axis=1)
)

output:

  CREDIT.HISTORY.LENGTH  MONTHS
0             0yrs 0mon       0
1             2yrs 6mon      30
2             0yrs 0mon       0
3             0yrs 0mon       0
4             3yrs 1mon      37
5            15yrs 2mon     182
6            1yrs 10mon      22
7             0yrs 0mon       0
8             0yrs 0mon       0
9            3yrs 10mon      46
  • Related