如何在python中拆分字符串并只取第一个字符串(一个数字)



我的数据帧中有这一列,我想把数字和字符串分开,只取1、2、3和4。

type
2 type (a, b)
4 type (a, b, c, d)
3 type (a, b, c)
1 CYCLE (a)

我所期望的:

type
2
4
3
1

我应该如何编写脚本才能拆分数字并获得期望的列?

提前感谢

如果"type"列中的所有数字都是number type....:格式,则也可以使用split()

df['type']=df['type'].str.split(' ',1).str[0]

str.extract与正则表达式模式^d+:一起使用

df["num"] = df["type"].str.extract(r'^(d+)')