在python数据表不是pandas数据框上应用用户定义的函数?

问题描述

数据表在R中很流行,但它也有一个Python version。但是,我在文档中看不到将用户定义的函数应用于数据表的任何内容

这是一个玩具示例(以熊猫为例),其中将用户功能应用于数据框以查找邮箱地址:

df = pd.DataFrame({'customer':[101,102,103],'address':['12 main st','32 8th st,7th fl','po Box 123']})

customer | address
----------------------------
101      | 12 main st
102      | 32 8th st,7th fl
103      | po Box 123


# user-defined function:
def is_poBox(s):
    rslt = re.search(r'^p(ost)?\.? *o(ffice)?\.? *Box *\d+',s)
    if rslt:
        return True
    else:
        return False

# Using .apply() for this example:
df['is_poBox'] = df.apply(lambda x: is_poBox(x['address']),axis = 1)

# Expected Output:
customer | address          | rslt
----------------------------|------
101      | 12 main st       | False
102      | 32 8th st,7th fl| False
103      | po Box 123       | True

是否可以在datatable中执行此.apply操作?会很好,因为对于大多数操作,datatable似乎比pandas快很多。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)