Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame apply() ValueError: too many values to unpack (expected 2)

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))

but getting:

---------------------------------------------------------------------------
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?

Any feedback will be appreciated.

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'
like image 912
Irek Rybark Avatar asked Feb 12 '16 22:02

Irek Rybark


1 Answers

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

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
like image 89
Kevin Avatar answered Sep 30 '22 02:09

Kevin