热线电话:13121318867

登录
2019-01-24 阅读量: 2453
从列中的字符串中提取一组n个数字

我在pandas数据框中有一列字符串,其中包含以下内容:"AU/4347001"但另外还有其他字符串组织较少,如"Who would have thought this would be so 4347009 difficult"

因此,最终,这些数字系列在字符串中的位置和方式没有连贯的模式。它们可能位于开头,中间或末尾,并且无法准确知道数字周围有多少其他字符。

理想情况下,我想返回另一个长度相等但只包含数字的列。

0.0000
1
关注作者
收藏
评论(1)

发表评论
啊啊啊啊啊吖
2019-01-24

可以这样做extract:

df =pd.DataFrame({'text':["Who would have thought this would be so 4347009 difficult",

"24 is me"]})

df['new_col'] = df['text'].str.extract(r'(\d+)')

text new_col

0 Who would have thought this would be so 434700... 4347009

1 24 is me 24

0.0000 0 0 回复