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

Time:04-20

0yrs 0mon

2yrs 6mon

0yrs 0mon

0yrs 0mon

3yrs 1mon

15yrs 2mon

1yrs 10mon

0yrs 0mon

0yrs 0mon

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
  • Related