Pandas - Json File Data 분석 1(기본 문법 사용)
1. Sample Data
usa.gov 사이트를 방문한 데이터들을 JSON 형식으로 작성한 데이터
JSON(Javascript Object Notation) : 자바스크립트 객체 표현 방법
JSON은 Python의 dict 데이터 타입과 비슷(동일)
{key1 : value1, key2 : value2 ...}
2. import Module
import json
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from print_df import print_df
3. Python Basic Code
- Data File 경로 지정
path = 'data\example.txt'
- File 확인
print(open(path, encoding='utf-8').read())
- List comprehension(json.loads(문자열) : json 형식의 문자열을 python의 dict 타입으로 변환)
records = [json.loads(line) for line in open(path, encoding='utf-8')]
- records에 0번 라인 확인
print(records[0])
{'a': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 Safari/535.11', 'c': 'US', 'nk': 1, 'tz': 'America/New_York', 'gr': 'MA', 'g': 'A6qOVH', 'h': 'wfLQtf', 'l': 'orofrog', 'al': 'en-US,en;q=0.8', 'hh': '1.usa.gov', 'r': 'http://www.facebook.com/l/7AQEFzjSi/1.usa.gov/wfLQtf', 'u': 'http://www.ncbi.nlm.nih.gov/pubmed/22415991', 't': 1331923247, 'hc': 1331822918, 'cy': 'Danvers', 'll': [42.576698, -70.954903]}
- records 0번 라인 'a' Key에 Value 확인
print(records[0]['a'])
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 Safari/535.11
print(type(records[0]))
print(len(records))
3560
time_zone = [rec['tz'] for rec in records if 'tz' in rec]
- time_zone 길이 확인
print(len(time_zone))
3440
- time_zone 일부 데이터 확인
print(time_zone[0:5])
['America/New_York', 'America/Denver', 'America/New_York', 'America/Sao_Paulo', 'America/New_York']
def get_count(sequence):
counts = {} # '원소 : 갯수'들로 이루어진 dict
for x in sequence: # 리스트의 모든 원소를 검사
if x in counts: # 리스트의 원소가 dict의 키로 존재하면
counts[x] += 1 # 기존 갯수 + 1을 저장
else:
counts[x] = 1 # 1을 저장
return counts
- time_zone의 원소 갯수 세기
counts = get_count(time_zone)
{'America/New_York': 1251, 'America/Denver': 191, 'America/Sao_Paulo': 33, 'Europe/Warsaw': 16, '': 521, 'America/Los_Angeles': 382, 'Asia/Hong_Kong': 10, 'Europe/Rome': 27, 'Africa/Ceuta': 2, 'Europe/Madrid': 35, 'Asia/Kuala_Lumpur': 3, 'Asia/Nicosia': 1, 'Europe/London': 74, 'Pacific/Honolulu': 36, 'America/Chicago': 400, 'Europe/Malta': 2, 'Europe/Lisbon': 8, 'Europe/Paris': 14, 'Europe/Copenhagen': 5, 'America/Mazatlan': 1, 'Europe/Dublin': 3, 'Europe/Brussels': 4, 'America/Vancouver': 12, 'Europe/Amsterdam': 22, 'Europe/Prague': 10, 'Europe/Stockholm': 14, 'America/Anchorage': 5, 'Asia/Bangkok': 6, 'Europe/Berlin': 28, 'America/Rainy_River': 25, 'Europe/Budapest': 5, 'Asia/Tokyo': 37, 'Europe/Vienna': 6, 'America/Phoenix': 20, 'Asia/Jerusalem': 3, 'Asia/Karachi': 3, 'America/Bogota': 3, 'America/Indianapolis': 20, 'America/Montreal': 9, 'Asia/Calcutta': 9, 'Europe/Skopje': 1, 'Asia/Beirut': 4, 'Australia/NSW': 6, 'Chile/Continental': 6, 'America/Halifax': 4, 'America/Edmonton': 6, 'Europe/Bratislava': 3, 'America/Recife': 2, 'Africa/Cairo': 3, 'Asia/Istanbul': 9, 'Asia/Novosibirsk': 1, 'Europe/Moscow': 10, 'Europe/Sofia': 1, 'Europe/Ljubljana': 1, 'America/Mexico_City': 15, 'Europe/Helsinki': 10, 'Europe/Bucharest': 4, 'Europe/Zurich': 4, 'America/Puerto_Rico': 10, 'America/Monterrey': 1, 'Europe/Athens': 6, 'America/Winnipeg': 4, 'Europe/Riga': 2, 'America/Argentina/Buenos_Aires': 1, 'Asia/Dubai': 4, 'Europe/Oslo': 10, 'Asia/Yekaterinburg': 1, 'Asia/Manila': 1, 'America/Caracas': 1, 'Asia/Riyadh': 1, 'America/Montevideo': 1, 'America/Argentina/Mendoza': 1, 'Asia/Seoul': 5, 'Europe/Uzhgorod': 1, 'Australia/Queensland': 1, 'Europe/Belgrade': 2, 'America/Costa_Rica': 1, 'America/Lima': 1, 'Asia/Pontianak': 1, 'America/Chihuahua': 2, 'Europe/Vilnius': 2, 'America/Managua': 3, 'Africa/Lusaka': 1, 'America/Guayaquil': 2, 'Asia/Harbin': 3, 'Asia/Amman': 2, 'Africa/Johannesburg': 1, 'America/St_Kitts': 1, 'Pacific/Auckland': 11, 'America/Santo_Domingo': 1, 'America/Argentina/Cordoba': 1, 'Asia/Kuching': 1, 'Europe/Volgograd': 1, 'America/La_Paz': 1, 'Africa/Casablanca': 1, 'Asia/Jakarta': 3, 'America/Tegucigalpa': 1}
def top_counts(count_dict, n=10):
cnt_tz_pair = [(count, tz) for tz, count in count_dict.items()] # 정렬할때 첫번째를 가지고 정렬하므로 위치를 변경
cnt_tz_pair.sort() # count를 기준으로 정렬
return cnt_tz_pair[-n:] # 정렬된 데이터에서 마지막 n개
- 지역 10개 추출 출력(기본 파라미터 사용)
print(top_counts(counts))
[(33, 'America/Sao_Paulo'), (35, 'Europe/Madrid'), (36, 'Pacific/Honolulu'), (37, 'Asia/Tokyo'), (74, 'Europe/London'), (191, 'America/Denver'), (382, 'America/Los_Angeles'), (400, 'America/Chicago'), (521, ''), (1251, 'America/New_York')]
print(top_counts(counts, n=5))
[(191, 'America/Denver'), (382, 'America/Los_Angeles'), (400, 'America/Chicago'), (521, ''), (1251, 'America/New_York')]