글

5월, 2020의 게시물 표시

[Python_bigdata] 파이썬 판다스Pandas 기본 개념 및 활용

#day06_03_pandas.py # -pandas # 판다스 예제 # 판다스 이용하기 및 자료구조  import pandas as pd import numpy as np pd.__version__ #Series 정의 obj=pd.Series([1,2,3,4,5])  obj #Serise 의 값 확인 obj.values #Series 의 인덱스 확인 obj.index #Series 연산 #모든 자리에 3을 더해라  obj + 3 #평균  obj.mean() ##데이터 프레임 만들기  #비정형 데이터  예) list  [1,3] #대표적인 비정형 데이터라고 볼 수 있다 . #반정형 데이터 : 데이터에 대해 순번 또는 키 등을 알 수 있으나 테이블의 형태는 아니다. key와 value로 이뤄진 형태. my_dict={'a':[1,3],'b':[1,2],'c':[2,4]} my_dict #정형 데이터 : 반정형과의 가장 큰 차이는 행과 열을 가지는 테이블의 형태를 가진다.  pd.DataFrame(my_dict) #리스트 타입으로 데이터 프레임 만들기 mylist = [['tom',20],['nick',30],['korea','40']] mylist a= pd.DataFrame(mylist,columns=['name','age']) #columns 를 통해 열 이름 지정 #DataFrame 의 조회 ## loc : 라벨값 기반의 2차원 인덱싱 ## iloc : 인덱스 기반의 2차원 인덱싱 ## at :라벨값 기반의 인덱싱 (데이터1개 ) ## iat : 인덱스 기반의 인덱싱 ( 데이터1개 ) my_dict={'a':[1,4,7],'b':[2,5,8],'c':[3,6,9]} df = pd.DataFrame(my_dict) #df.loc[행,열]  display(df.loc[:,:]) #전체 dis...

[Python_bigdata] 파이썬을 활용한 크롤링(여러 개의 투입값에 따른 결과값 반환)

검색어들을 리스트로 저장 후 해당 검색어에 따른 횟수를 반복문을 통해 받아오는 방식이다. input()을 이용할 수도 있을 것이다. #day06_02_crawling.py from bs4 import BeautifulSoup import requests as req keywords  = ['빅데이터','머신러닝','마스크'] for i in keywords:     url = 'http://jtbc.joins.com/search/news?term='+i     response = req.get(url)     soup = BeautifulSoup(response.text,'html.parser')     cnt = soup.select('.txt_result')[0]     print(i,':',cnt.select('strong')[0].text,'건')     #print({},':',{},'건').format(i,cnt.select('strong')[0].text) #.format 활용

[Python_bigdata] 파이썬을 활용한 크롤링

#day06_01_crawling.py #크롤링 예제, jtbc 뉴스검색 사이트에서 검색어에 따른 기사 갯수 구하기 from bs4 import BeautifulSoup import requests as req keyword  = '빅데이터' url = 'http://jtbc.joins.com/search/news?term='+keyword #url 주소에 요청(request) response = req.get(url) response.text #html #응답 받은 response 객체의 text를 html 형태로 반환 soup = BeautifulSoup(response.text,'html.parser') # '.'은 class, '#'은 id soup.select('.txt_result')[0] #list 형태이기 때문에 첫번째 인덱스 값을 가져올 것이다.  #<strong> 태그 내부의 글자가 필요하다. cnt = soup.select('.txt_result')[0] cnt.select('strong')[0].text #리스트 인덱스 0 의 내용 중 텍스트 값만 뽑아올 수 있다.  print(keyword,':',cnt.select('strong')[0].text,'건')

[Python_bigdata] 파이썬을 활용한 웹 크롤링

# Day05_04_crawling # !/usr/bin/env python # coding: utf-8   # 크롤링으로 원하는 데이터 수집 # python 에서 크롤링을 위해서 필요한 외부 패키지 import requests as req from bs4 import BeautifulSoup # 1. 대상사이트 target_url =' https://www.apache.org/' # 2. 대상 사이트에 통해 페이지 요청하기 response = req.get(target_url) # get 요청에 따른 response 200 이면 성공 # 3. 응답된 결과를 담은 response 변수에서 페이지의 내용보기 BeautifulSoup(response.text) # response.text 는 문자 자체라고 볼 수 있다 . BeautifulSoup 를 통해 html 과 같이 가져온다 . soup = BeautifulSoup(response.text,'html.parser')  #html.parser를 이용하여 html 형태로 출력가능 # 4. html 결과값에서 긁어올 부분 ( 가져올 부분 ) select 하기 categories = soup.select(' # by_category>ul>li>a') # ,> 를 통해 하위의 코드를 가져온다 . categories   # categories 를 반복문을 통해 순회한다 . for i in categories:     print(i.text) # <a 내부의 텍스트만 추출   categories2 = soup.select('.col-lg-6>ul>li>a') # ,> 를 통해 하위의 코드를 가져온다 . categories2 for i in categories2:     print(i.te...

[Python_bigdata] 파이썬 기본 문법 (연산, 반복문, 조건문, 배열, 리스트, 함수, 패키지)

# Day05_01_basic # !/usr/bin/env python # coding: utf-8   # day05_01_data_type   # 주석 print('hello ,world')   # 데이터 타입 # 숫자형 # 정수형 a =123 b=-123 print(a) print(b)   # 실수형 c=123.123 d=100.000 print(c) print(d) print(c-d) # 실수형은 정확하게 계산을 시킬 수 없는 영역이기 때문에 뒤에 0000001 이 붙는다 .   123.123 == 123.123   # 문자형을 만드는 법 print('') print("") a="""   가독성 좋게 하는 법     """ print(a)   # 파이썬은 문자형이 존재하지 않는다 . # 문자열만 존재한다 .   a='hello world' print(a) print(a[0]) print(a[6]) print(a[0:6]) print(a[:]) print(a[0:]) print(a[:7]) print(a[7:])   # 실습 w 를 W 로 변경 # 조합을 해서 변경 w='w' w=w.upper() print(a[:6]+'W'+a[7:]) print(a[:6]+w+a[7:])   a='hello' b='korea' print(a,b)   # LIST # 여러가지 데이터 형을 동시에 가지고 있는 구조 # 파이썬은 배열형이 존재하지 않는다 . # 만드는 방법 a=[1,2,3] a[1]   b=['a','b',...