Python/Python For Analytics
-
[Python] Pandas DataFrame 컬럼명 특정 문자로 변경Python/Python For Analytics 2019. 9. 25. 16:04
import pandas as pd df = pd.DataFrame({'A-1':[1,2,3,4,5],'A-2':[1,2,3,4,5]}) print(df.columns) --------------------------------------------------------- Index(['A-1', 'A-2'], dtype='object') # columns.str.replace(변경전 문자,변경할 문자) df.columns = df.columns.str.replace('-','_') print(df.columns) --------------------------------------------------------- Index(['A_1', 'A_2'], dtype='object') # - A-1이 A_..
-
[Python] pandas datetime 타입 시간/주/일 더하기Python/Python For Analytics 2019. 9. 6. 13:38
시스템 로그를 분석할 때, 로그시간에 UTC시간을 더해줘야 할 때가 있는데, datetime의 timedelta의 메소드를 이용하여 변환할 수 있겠다. 기준일 from datetime import datetime, timedelta ....... print(data['time']) 0 2019-08-27 00:00:00 1 2019-08-27 00:00:00 2 2019-08-27 00:00:00 3 2019-08-27 00:00:00 4 2019-08-27 00:00:00 ... 373064 2019-08-27 23:59:58 373065 2019-08-27 23:59:59 373066 2019-08-27 23:59:59 373067 2019-08-27 23:59:59 373068 2019-08-27 ..
-
[Python] list data type pandas의 DataFrame 만들기Python/Python For Analytics 2019. 9. 4. 20:32
1개 의 리스트 데이터를 DataFrame만들기 import pandas as pd lst_A = ['a','b','c','d'] df = pd.DataFrame(lst_A) print(df) ------------------------------- 0 0 a 1 b 2 c 3 d 2개이상의 동일한 길이의 리스트 데이터를 DataFrame만들기 ( zip 활용) import pandas as pd lst_A = ['a','b','c','d'] lst_B = [1,2,3,4] df = pd.DataFrame([ x for x in zip(lst_A,lst_B)]) print(df) ------------------------------------------------- 0 1 0 a 1 1 b 2 2 c ..
-
[Python] pandas를 이용한 IIS log 파싱Python/Python For Analytics 2019. 8. 30. 20:19
python pandas를 이용한 iis weblog 파싱 import pandas as pd log_field = ['date', 'time', 's-sitename', 's-computername' , 's-ip' , 'cs-method' , 'cs-uri-stem', 'cs-uri-query', 's-port' ,'cs-username', 'c-ip', 'cs-version', 'cs-User-Agent', 'cs-Cookie', 'cs-Referer', 'cs-host', 'sc-status', 'sc-substatus', 'sc-win32-status', 'sc-bytes', 'cs-bytes', 'time-taken'] df = pd.read_csv('logfile', sep=' ', comm..
-
[Python] pandas를 이용한 mariadb 결과값 다른 mariadb 테이블로 저장Python/Python For Analytics 2019. 8. 29. 01:59
python pandas를 이용해서 mariadb의 쿼리 결과값을 다른 table로 저장이 가능하다. 샘플로 perfomance_log 테이블의 4개 컬럼 1,000,000를 불러와서 새로운 테이블로 저장해보겠다. sys_processortime sys_mem_availablembytes sys_net_revbytes_sec sys_net_sendbytes import pandas as pd import pymysql from sqlalchemy import create_engine conn = pymysql.connect(host='host', user='user', password='password' ,db='db', charset='utf8') query = 'select sys_processort..
-
[Python] dataframe of pandas returns mysql / Maria DB resultPython/Python For Analytics 2019. 8. 8. 10:20
mysql / maria DB 쿼리의 결과를 pandas의 dataframe으로 만들기 import pymysql import pandas as pd db = pymysql.connect( host=' ', port= , user=' ', password=' ', db=' ', charset='utf8' ) SQL = "select * from table" df = pd.read_sql(SQL,db) print(df)
-
[Python] Retrieving data from all sheets of Excel file using openpyxl and pandasPython/Python For Analytics 2018. 6. 10. 00:58
openpyxl과 pandas를 이용한 엑셀파일의 모든 시트의 데이터 불러오기 import openpyxl import pandas as pd xlsxFile = 'D:\\test\\files.xlsx' sheetList = [] # openpyxl를 이용하여 시트명 가져오기 wb = openpyxl.load_workbook(xlsxFile) for i in wb.get_sheet_names(): sheetList.append(i) # pandas를 이용하여 각 시트별 데이터 가져오기 xlsx = pd.ExcelFile(xlsxFile) for j in sheetList: df = pd.read_excel(xlsx, j) print('%s Sheet의 데이타 입니다.' %j) print(df) print..