我刚刚开始探索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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。