我将sha1_vsdt.csv文件与日志文件匹配,如果csv中名为“ SHA1”的第一列与日志文件匹配,请获得第11列,否则填充“未检测到”。
这是我的代码,它适用于某些日志文件,但是当我尝试使用此trendx.log时,它会在下面显示错误
# I used the 10th column as sha1 and 11th column as as description
import pandas as pd
import numpy as np
import csv
logtext = "trendx.log"
logdata = np.genfromtxt(log_file + "\\" + logtext,invalid_raise = False,dtype=object, comments=None,usecols=np.arange(16),encoding='utf_16-le')
logframe = pd.DataFrame(logdata)
df2=(logframe[[10,11]]).rename(columns={10:'SHA-1', 11: 'DESC'})
df1=pd.read_csv(out_sha1_vsdt ,delimiter=",",error_bad_lines=False,engine = 'python',quoting=3)
df = pd.merge(df1, df2, on='SHA-1', how='left').fillna('Undetected')
df1['DESC'] = df['DESC'].values
我收到此错误:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\OJT\scanner\match.py", line 33, in <module>
df1['DESC'] = df['DESC'].values
File "C:\Users\Administrator\AppData\Roaming\Python\Python27\site-packages\pandas\core\frame.py", line 3119, in __setitem__
self._set_item(key, value)
File "C:\Users\Administrator\AppData\Roaming\Python\Python27\site-packages\pandas\core\frame.py", line 3194, in _set_item
value = self._sanitize_column(key, value)
File "C:\Users\Administrator\AppData\Roaming\Python\Python27\site-packages\pandas\core\frame.py", line 3391, in _sanitize_column
value = _sanitize_index(value, self.index, copy=False)
File "C:\Users\Administrator\AppData\Roaming\Python\Python27\site-packages\pandas\core\series.py", line 4001, in _sanitize_index
raise ValueError('Length of values does not match length of ' 'index')
ValueError: Length of values does not match length of index
csv中正确的输出应如下所示:
0665de1ad83715cc6e68d00ed700c469944a5925,WIN32 EXE 7-2,Undetected
067b448f4c9782489e5ff60c31c62b7059e500b2,Adobe Portable Document Format(PDF) 6015-0,Not Supported
0688e6966b0e4a1f58d2f3de48f960fce5b42292,MS Office 1-0,Not Supported
0689f6f99d10dd8bf396f2d2c73ce9dcb6dcad23,ASCII text 18-0,Undetected
06a60c6018a42b1db22e3bf8620861711401c4bb,WIN32 EXE 7-2,TROJ.Win32.TRX.XXPE50FFF026
0723a895a5f8b2d5d25b4303e9f04d16551791b6,MSIL 7-18,TROJ.Win32.TRX.XXPE50FFF026
07344621cf4480c430f8931af2b2b056775af7e3,MIME 6010-0,Undetected
07831df482f1a34310fc4f5a092c333eeaff4380,Microsoft RTF 6008-0,Not Supported
08386105057cd5867480095696a5ca6701fdb8ad,ASCII text 18-0,Undetected
编辑:我发现我只是删除了.values
中的df1['DESC'] = df['DESC'].values
,但没有给我正确的输出
答案 0 :(得分:1)
替换
pd.merge(df1, df2, on='SHA-1', how='left').fillna('Undetected')
在下面一行
pd.merge(df1,df2,on=['SHA-1','DESC'],how='outer').fillna('undetected')
在上面的行how
参数中,我使用了outer
,根据您的要求可以为left, right, outer, inner
。
正如您在第
df1['DESC'] = df['DESC'].values
应更改为
df1['DESC'] = df['DESC']