python获取字符串长度,如何从python中的字符串中提取一定长度的数字?

I have a dataframe which looks like this:

description

1906 RES 330 ML

1906 RES 330ML

RES 335 c/6

RES 332 c/12

I want to extract the three consecutive digits of numbers and save it in a new column 'volume'.

My code is like this:

df['volume'] = df['description'].str.extract('([([\d]*[\d]){3,3}?])')

EXPECTED RESULTS SHOULD BE LIKE THIS:

volume

330

330

335

332

However, it gives the results like this:

volume

1906

1906

335

332

Can anyone help me fix this code? Thanks so much!!!

解决方案

Might be overkill, but if you want to make sure you don't capture numbers that are part of 4 digit numbers, you might use this:

df['volume'] = df.description.str.extract(r'(?

print(df)

description volume

0 1906 RES 330 ML 330

1 1906 RES 330ML 330

2 RES 335 c/6 335

3 RES 332 c/12 332

Specify expand=False, so that matches are returned as one pd.Series only.

The regex:

(?

(\d{3}) - matches 3 digits

(?!\d) - specifies that anything after a set of 3 digits is something that is not a digit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值