2017-02-22 4 views
1

IP 범위 테이블 내에서 해당 위치의 이름과 해당 위치의 시작 IP 주소가 있습니다. 규칙은 다음과 같습니다. 다음 행이 동일한 주소 범위에있는 경우 위치의 끝 IP 주소는 다음 행의 값 -1입니다. 그렇지 않으면 해당 범위의 마지막 주소입니다. 여기 팬더의 새로운 열 값에 대한 시프트 사용

Name StartRange  EndIP 
loc1 172.28.10.15 172.28.10.127 
loc2 172.28.10.128 172.28.10.255 
loc3 172.28.12.0  172.28.12.57 
loc4 172.28.12.58 172.28.12.255 

내가하려고했던 코드입니다 :

Name StartRange 
loc1 172.28.10.15 
loc2 172.28.10.128 
loc3 172.28.12.0 
loc4 172.28.12.58 

예상 결과는 다음과 같습니다

from socket import inet_aton 
from struct import unpack 

import pandas as pd 

mask = unpack(">L", inet_aton('255.255.255.0'))[0] 

def getEndIP(startIP, endIP): 
    hi = (startIP['StartIP'] & mask) + 255 
    return hi if hi < endIP['StartIP'] else endIP['StartIP'] - 1 

xls = pd.read_excel("E:\\TEMP\\AllScope.xlsx") 
xls['StartIP'] = xls['StartRange'].map(lambda a: unpack(">L", inet_aton(a))[0]) 
xls = xls.sort_values('StartIP') 
xls['EndIP'] = getEndIP(xls['StartIP'], xls['StartIP'].shift(-1)) 

print xls[['Name', 'StartRange', 'StartIP', 'EndIP']] 

하지만 중요한 오류 메시지가 :

을 여기 는 샘플 데이터입니다
KeyError: 'StartIP' 

내가 할 일 틀렸어? 여기

runfile('E:/Documents/Projects/Python/Egyéb progik/Network/network.py', wdir='E:/Documents/Projects/Python/Egyéb progik/Network') 
Traceback (most recent call last): 

    File "<ipython-input-67-6caaa536457c>", line 1, in <module> 
    runfile('E:/Documents/Projects/Python/Egyéb progik/Network/network.py', wdir='E:/Documents/Projects/Python/Egyéb progik/Network') 

    File "C:\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile 
    execfile(filename, namespace) 

    File "C:\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile 
    exec(compile(scripttext, filename, 'exec'), glob, loc) 

    File "E:/Documents/Projects/Python/Egyéb progik/Network/network.py", line 15, in <module> 

    File "E:/Documents/Projects/Python/Egyéb progik/Network/network.py", line 9, in getEndIP 

    File "C:\Anaconda2\lib\site-packages\pandas\core\series.py", line 603, in __getitem__ 
    result = self.index.get_value(self, key) 

    File "C:\Anaconda2\lib\site-packages\pandas\indexes\base.py", line 2169, in get_value 
    tz=getattr(series.dtype, 'tz', None)) 

    File "pandas\index.pyx", line 98, in pandas.index.IndexEngine.get_value (pandas\index.c:3557) 

    File "pandas\index.pyx", line 106, in pandas.index.IndexEngine.get_value (pandas\index.c:3240) 

    File "pandas\index.pyx", line 156, in pandas.index.IndexEngine.get_loc (pandas\index.c:4363) 

KeyError: 'StartIP' 
+0

어떤 줄이 오류를 제기합니까? –

+0

xls [ 'EndIP'] = getEndIP (xls [ '시작 IP'], xls [ '시작 IP']. 이동 (-1)) 또는 getEndIP 함수 자체 – Gabor

+1

xls [ 'StartIP']' 'getEndIP()'를 호출 한 다음 그 함수에서''StartIP'' 컬럼에 다시 접근하십시오. 즉, xls [ 'StartIP'] [ 'StartIP']'를 찾고 있습니다. – chrisaycock

답변

0

팬더 솔루션 : dfl를 가정하면 우리의 첫 번째 문자열을 번역

  StartRange 
Name    
loc1 172.28.10.15 
loc2 172.28.10.128 
loc3 172.28.12.0 
loc4 172.28.12.58 

여기 추적입니다 :

업데이트 (내가 팬더와 아직 너무 익숙하지 않다) 산술에 대한 int리스트 :

dfl['StartRange']= dfl.StartRange.apply(lambda s : [int(x) for x in s.split('.')]) 
dfl['EndIP']=dfl.StartRange.shift(-1) 
dfl.ix[-1,'EndIP']=[255,255,255,255] 

def adjust(row): 
    start,end=row 
    return min(start[:3]+[255],end[:3]+[end[3]-1]) 

dfl['EndIP']=dfl.apply(adjust,axis=1) 
dfend=dfl.applymap(lambda l : '.'.join([str(x) for x in l])) 

그런 다음 dfend

  StartRange   EndIP 
Name        
loc1 172.28.10.15 172.28.10.127 
loc2 172.28.10.128 172.28.10.255 
loc3 172.28.12.0 172.28.12.57 
loc4 172.28.12.58 172.28.12.255 
+0

추적으로 업데이트되었습니다. 첫 번째 행에 오류가 있습니다. ValueError : ("int()에 대한 리터럴이 10 진수이고 : 'loc1'", 인덱스에 u'ccurred가 있습니다. 이름 ') – Gabor

+0

이름은 색인이어야합니다. 전에 dfl.set_index ('Name')를 시도하십시오. –

관련 문제