用正则表达式提取整数的函数返回nonetype [英] function to extract integer with regex returns nonetype

查看:198
本文介绍了用正则表达式提取整数的函数返回nonetype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个从字符串中提取整数的函数.字符串示例如下,它是我数据框中的一列. 我得到的输出在方括号中,里面有很多数字.我想使用这些数字进行进一步的计算,但是当我检查它是什么时,它是一个Nonetype而不是整数.这是为什么?以及如何将其转换为整数,以便可以使用得到的输出编号找到.sum()或.mean()?理想情况下,我希望将提取的整数作为另一个列,例如str.extract(regex,inplace = True).

I wrote a function to extract integer from strings. The strings example is below and it is a column in my dataframe. The output I got is in square bracket, with a lot of numbers inside. I want to use those numbers to compute further, but when I check what it is, instead of integer, it is a Nonetype. Why is that? and how can I convert it into integer so I can find .sum() or .mean() with the output numbers I got? Ideally, I want the extracted integer as another column like with str.extract(regex, inplace=True).

这是我的数据的一部分,它是我的数据框df2017中的一列

Here is part of my data, which is a column in my dataframe df2017

Bo medium lapis 20 cash pr gr
Porte monnaie dogon vert olive 430 euros carte
Bo noires 2015 fleurs clips moins brillant 30 ...
Necklace No 20 2016 80€ carte Grecs 20h00 salo...
Bo mini rouges 30 carte 13h it
Necklace No 17 2016 100€ cash pr US/NYC crois ...
Chocker No 1 2016 + BO No 32 2016 70€ cash pr …

这是我的代码

def extract_int_price():
    text=df2017['Items'].astype(str)
    text=text.to_string()
    amount=[int(x) for x in re.findall('(?<!No\s)(?<!new)(?!2016)(\d{2,4})+€?', text)]
    print (amount)

谢谢!

推荐答案

您的函数返回None,因为您忘记了return语句.因为Python中的每个函数都有一个返回值,所以缺少的return语句就像返回None.

Your function returns None because you forgot the return statement. Because every function in Python has a return value, a missing return statement is like returning None.

这篇关于用正则表达式提取整数的函数返回nonetype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆