将"10yrs 5mon"的分类值转换为月

编程入门 行业动态 更新时间:2024-10-27 00:28:08
本文介绍了将"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"的分类值转换为月

本文发布于:2023-10-27 03:46:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532215.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   quot   yrs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!