Pandas DataFrame apply()ValueError:太多值无法解包(预期2) [英] Pandas DataFrame apply() ValueError: too many values to unpack (expected 2)

查看:118
本文介绍了Pandas DataFrame apply()ValueError:太多值无法解包(预期2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始研究Python,虽然我很兴奋,但似乎与Python无关.

I just started poking around Python and while I am very excited, it seems that I am far from pythonian thinking.

这是方法的一个示例,该方法到处都有次优"一词. 尽管这对于我相对较小的数据集已经足够了,但我想知道如何编写更好的方法?

Here is an example of approach, which has word 'suboptimal' all over. While this is sufficient for my relatively small dataset, I am wondering how can I write it better way?

import pandas as pd
from pandas import DataFrame

# create sample log data frame
lg = pd.DataFrame(['Access violation at address 00A97...',
                   'Try to edit the splines or change...',
                   'Access violation at address 00F2B...',
                   'Please make sure the main electro...'], columns=['lg_msg'])

# define message classification
err_messages = [['Access violation', 'ACC-VIOL', 'PROG'],
                ['Please make sure th', 'ELE-NOT-PLACED', 'MOD'],
                ['Try to edit the splines', 'TRY-EDIT-SPLINES', 'MOD']]                

# lookup code
def message_code(msg_text):
    for msg in err_messages:
        if msg_text.startswith(msg[0]):
            return msg[1]
    return ''

# lookup type
def message_type(msg_text):
    for msg in err_messages:
        if msg_text.startswith(msg[0]):
            return msg[2]
    return ''               

lg['msg_code'] = lg['lg_msg'].apply(lambda x:  message_code(x))
lg['msg_type'] = lg['lg_msg'].apply(lambda x:  message_type(x))

我尝试创建一个函数来计算日志输入代码并立即输入:

I tried creating a single function to calculate log entry code and type at once:

def message_code_type(msg_text):
    for msg in err_messages:
        if msg_text.startswith(msg[0]):
            return (msg[1], msg[2])
    return ('', '')

lg['msg_code'], lg['msg_type'] = lg['lg_msg'].apply(lambda x:  message_code_type(x))

但得到:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-72f97d857539> in <module>()
----> 1 lg['msg_code'], lg['msg_code'] = lg['lg_msg'].apply(lambda x:  message_code_type(x))

ValueError: too many values to unpack (expected 2)

有什么方法可以不遍历数据帧两次?

Is there any way to not traverse the dataframe twice?

任何反馈将不胜感激.

import sys
print(sys.version)
3.5.1 |Anaconda 2.4.0 (64-bit)| (default, Jan 29 2016, 15:01:46) [MSC v.1900 64 bit (AMD64)]

pd.__version__
'0.17.1'

推荐答案

使用 izip (来自itertools模块):

try this using izip from the itertools module:

from itertools import izip
lg['msg_code'], lg['msg_code'] = izip(*lg['lg_msg'].apply(lambda x:  message_code_type(x)))

In [21]:    lg
Out[21]:
    lg_msg  msg_code
0   Access violation at address 00A97...    PROG
1   Try to edit the splines or change...    MOD
2   Access violation at address 00F2B...    PROG
3   Please make sure the main electro...    MOD

对不起,对于2.7,您应该只能使用内置的 zip

Sorry, thats for 2.7, you should just be able to use the built-in zip

lg['msg_code'], lg['msg_type'] = zip(*lg['lg_msg'].apply(lambda x:  message_code_type(x)))

    lg_msg  msg_code    msg_type
0   Access violation at address 00A97...    ACC-VIOL    PROG
1   Try to edit the splines or change...    TRY-EDIT-SPLINES    MOD
2   Access violation at address 00F2B...    ACC-VIOL    PROG
3   Please make sure the main electro...    ELE-NOT-PLACED  MOD

这篇关于Pandas DataFrame apply()ValueError:太多值无法解包(预期2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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