我刚刚开始探索
Python,虽然我很兴奋,但我似乎远非Python思维.
以下是一种方法示例,其中包含“次优”一词.
虽然这对我相对较小的数据集来说已经足够了,但我想知道如何更好地编写它?
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))
我尝试创建一个函数来计算日志条目代码并立即输入:
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)
有没有办法不两次遍历数据帧?
任何反馈将不胜感激.
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'
最佳答案 尝试使用itertools模块中的
izip
:
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
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