将复杂的str更改为漂浮在pandas Dataframe中

时间:2018-08-03 19:14:35

标签: python regex string pandas dataframe

我有一个数据集,其中包含具有if (v instanceof Map) { Map<String, Object> subMap = flatten((Map<String, Object>) v); subMap.keySet().stream().forEach(subkey -> result.put(k + "." + subkey, subMap.get(subkey))); } else result.put(k, v); 格式的'9 years 9 months 14 days'公司格式的公司资历的列。我通过str循环使用正则表达式将它们转换为float

for

有效。 但是,我对更有效,更快捷的方法感兴趣(如果存在)。

1 个答案:

答案 0 :(得分:2)

设置

SubClass

选项1
使用 MyParentClass

进行列表理解
df = pd.DataFrame(
    {'sen': ['9 years 9 months 14 days', '2 years 4 months 12 days']
})

选项2
str.findall df['seniority'] = [ sum((float(x), float(y)/12, float(z)/365)) for x, y, z in df.sen.str.findall(r'(\d+)').values ] # Result sen seniority 0 9 years 9 months 14 days 9.788356 1 2 years 4 months 12 days 2.366210 str.extract

div

时间

sum