본문 바로가기

전체 글140

[Python] json 파일 읽어서 csv로 저장하기 아래는 json 파일로 되어 있는 파일을 파이썬으로 읽어 엑셀로 저장하기 위한 코드이다. 우선 json은 파이썬의 딕셔너리처럼 키와 값, 두 쌍으로 이루어진 자료형식이다. 오픈 API를 사용하거나 크롤링을 진행할 때 자주 만나게 되고 아래 방법을 알기 전에는 엑셀로 전처리하기도 했는데 상당히 비효율적이고 휴먼에러가 발생한 여지가 있다. 그래서 아래와 같이 json파일을 읽어오면 휠씬 빠르게 가공하여 활용할 수 있다. import requests import pandas as pd import json url = '' #json URL response = requests.get(url) contents = response.text json_ob = json.loads(contents) body = json.. 2023. 1. 24.
[Python] 두 좌표사이의 거리 구하기 - Haversine distance 두 좌표(위도, 경도) 사이의 거리를 구할 때, 거리 측정 함수인 허버사인 거리(Haversine distance)을 사용하는데 지구의 지름을 6,371km로 두고 킬로미터 단위로 두 지점의 사이의 거리를 구해준다. 파이썬으로 나타내면 다음과 같다. from math import sin, cos, sqrt, atan2 def haversine_distance(lat1, lon1, lat2, lon2): R = 6371 # radius of Earth in kilometers dlat = lat2 - lat1 dlon = lon2 - lon1 a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2 c = 2 * atan2(sqrt(a), sqrt(.. 2023. 1. 19.
HackerRank SQL - The Report You are given two tables: Students and Grades. Students contains three columns ID, Name and Marks. Grades contains the following data: Ketty gives Eve a task to generate a report containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those students who received a grade lower than 8. The report must be in descending order by grade -- i.e. higher grades are entered first. .. 2023. 1. 18.
HackerRank SQL - Weather Observation Station 19 Consider and to be two points on a 2D plane where are the respective minimum and maximum values of Northern Latitude (LAT_N) and are the respective minimum and maximum values of Western Longitude (LONG_W) in STATION. Query the Euclidean Distance between points and and format your answer to display decimal digits. Input Format The STATION table is described as follows: where LAT_N is the northern.. 2023. 1. 12.
HackerRank SQL - New Companies Amber's conglomerate corporation just acquired some new companies. Each of the companies follows this hierarchy: Given the table schemas below, write a query to print the company_code, founder name, total number of lead managers, total number of senior managers, total number of managers, and total number of employees. Order your output by ascending company_code. Note: The tables may contain dupl.. 2023. 1. 9.
HackerRank SQL - Occupations Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output column headers should be Doctor, Professor, Singer, and Actor, respectively. Note: Print NULL when there are no more names corresponding to an occupation. Input Format The OCCUPATIONS table is described as follows: Occupation will only contain on.. 2023. 1. 6.
HackerRank SQL - The PADS Generate the following two result sets: Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S). Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascend.. 2023. 1. 3.
[Python] 네이버 플레이스 크롤링(selenium, BS4) 특정 지역의 데이터셋을 가지고 공부해보고 싶은게 있어서 지역 정보가 필요했다. 그런데 무식하게 긁어올 수 없어서 웹크롤러를 만들어보기로 했다. 바로 떠오른 생각은 네이버 지도에서 가져오는 것인데, 이게 생각보다 쉽지 않았다. 그래서 패쓰하고 네이버 플레이스 서비스로 접근했다. (OPEN API도 있었는데 전화번호 정보는 가져오지 못하는 한계가 있었다.) 우선 기본적인 라이브러리를 불러온다. 네이버에서는 일단 크롤링에 대해서 관대하지 않기 때문에 몇 가지 주의사항이 있다. from selenium.webdriver.common.by import By from urllib3.util.retry import Retry from requests.adapters import HTTPAdapter from bs4 .. 2022. 12. 20.
[Python] json 형식 읽기 import json from pandas.io.json import json_normalize json_string = ('json 문자열') 2022. 12. 14.