本文介绍了将"10yrs 5mon"的分类值转换为月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将列转换为数字作为预处理
converting the column to numeric as pre-processing
Aging '10yrs 1mon' '9yrs 8mon' '25yrs 5mon'预期:
'10yrs 1mon' 121 '9yrs 8mon' 116 '25yrs 5mon' 305推荐答案
使用 Series.str.extract ,将整数转换为新的DataFrame,并以多个12首先添加新列,然后添加第二列:
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更多推荐
将"10yrs 5mon"的分类值转换为月
发布评论