熊猫系列定期提取

时间:2019-02-09 17:34:20

标签: pandas

需要从Pandas列中提取以下值

8-9 yrs
7-12 yrs
4-6 yrs

该列中需要更新9,12,6。

2 个答案:

答案 0 :(得分:0)

str.extract与正则表达式结合使用,以获取-split之后的带有索引的数字,必要时最后转换为整数:

df['B1'] = df.A.str.extract('-(\d+)', expand=True)
df['B2'] = df.A.str.split(n=1).str[0].str.split('-').str[1].astype(int)
df['B3'] = df.A.str.split('-|\s+').str[1].astype(int)
print (df)
          A  B1  B2  B3
0   8-9 yrs   9   9   9
1  7-12 yrs  12  12  12
2   4-6 yrs   6   6   6

答案 1 :(得分:0)

具有df和一列a的DataFrame

enter image description here

使用带有re函数和正则表达式的findall

import re
df.a.apply(lambda x : re.findall(r'-(\d+)', x))

enter image description here