2014-11-13 11 views
0

이것은 간단한 질문 일 수 있습니다. 하지만 여기서 무슨 일이 일어나고 있는지를 파악하지 않고 많은 시간을 낭비했습니다. 리소스 확장을 기반으로 웹 로그 파일에서 HTTP 요청을 분류하려고합니다. 다음은 내가 시도한 것입니다.목록에있는 값을 기반으로 Pandas DataFrame 삽입 열

imgstr = ['.png','.gif','.jpeg','.jpg']  
docstr = [ '.pdf','.ppt','.doc' ] 
webstr = ['.html','.htm', '.asp', '.jsp', '.php', '.cgi', '.js','.css'] 
compressed = ['zip', 'rar', 'gzip', 'tar', 'gz', '7z'] 



def rtype(b): 
    if any(x in b for x in imgstr): 
     return 'A' 
    elif any(x in b for x in docstr): 
     return 'B' 
    elif 'favicon.ico'in b: 
     return 'C' 
    elif 'robots.txt'in b: 
     return 'D' 
    elif 'GET/HTTP/1.1' in b: 
     return 'E' 
    elif any(x in b for x in webstr): 
     return 'F' 
    elif any(x in b for x in compressed): 
     return 'G' 
    else: 
     return 'H' 

df2['result'] = df2.Request.apply(rtype) 

그러나 df2['result']'A'입니까? df2.Request의 데이터 형식은 Object입니다. 나는 df2['Referer'] = df2['Referer'].astype(str)으로 변경하려고 시도했다. 그래도 dtype은 Object입니다. 다음은 첫 번째 10 df2.Request입니다.

0,GET /index.php?lang=ta HTTP/1.1 
1,GET /index.php?limitstart=25&lang=en HTTP/1.1 
2,GET /index.php/ta/component/content/article/43 HTTP/1.1 
3,GET /index.php/ta/component/content/article/39-test HTTP/1.1 
4,GET /robots.txt HTTP/1.1 
5,GET /robots.txt HTTP/1.1 
6,GET /index.php/en/computer-security-feeds/15-computer-security/2-us-cert-cyber-security-alerts HTTP/1.1 
7,GET /index.php/component/content/article/10-tips/59-use-firefox-more-safe HTTP/1.1 
8,GET /robots.txt HTTP/1.1 
9,GET /onlinerenew/ HTTP/1.1 

답변

0

저는 이것을 위해 정규식을 사용하고 있습니다.

import pandas as pd 
import re 

def categoriser(x): 

if re.search('(.png|.gif|.jpeg|.jpg)', x): 
    return 'A' 
elif re.search('(.pdf|.ppt|.doc)', x): 
    return 'B' 
elif 'favicon.ico'in x: 
    return 'C' 
elif 'robots.txt'in x: 
    return 'D' 
elif 'GET/HTTP/1.1' in x: 
    return 'E' 
elif re.search('(.html|.htm|.asp|.jsp|.php|.cgi|.js|.css)', x): 
    return 'F' 
elif re.search('(zip|rar|gzip|tar|gz|7z)', x): 
    return 'G' 
else: 
    return 'H' 


string = """0,GET /index.php?lang=ta HTTP/1.1 
1,GET /index.php?limitstart=25&lang=en HTTP/1.1 
2,GET /index.php/ta/component/content/article/43 HTTP/1.1 
3,GET /index.php/ta/component/content/article/39-test HTTP/1.1 
4,GET /robots.txt HTTP/1.1 
5,GET /robots.txt HTTP/1.1 
6,GET /index.php/en/computer-security-feeds/15-computer-security/2-us-cert-cyber-security-alerts HTTP/1.1 
7,GET /index.php/component/content/article/10-tips/59-use-firefox-more-safe HTTP/1.1 
8,GET /robots.txt HTTP/1.1 
9,GET /onlinerenew/ HTTP/1.1"""  

frame = pd.DataFrame([x.split(",") for x in string.split("\n")]) 

print frame.loc[:,1].apply(categoriser) 

결과는 다음과 같습니다

0 F 
1 F 
2 F 
3 F 
4 D 
5 D 
6 F 
7 F 
8 D 
9 H 
Name: 1, dtype: object 

당신이 원하는 것 무엇인가요? 다음번에 원하는 출력을 포함 할 수 있다면 좋을 것입니다 :) dtype : object를 가진 것은 데이터 프레임에 기초한 numpy 배열이 문자열과 다른 stuff 객체들을 호출한다는 것입니다.이 경우 여전히 문자열입니다 :)

관련 문제