我刚刚开始探索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 '' …Run Code Online (Sandbox Code Playgroud)