멋사 ai 스쿨 TIL - (10) 웹스크래핑

eve·2022년 9월 27일
0

likeLion

목록 보기
10/45

-- 어제 배운 내용 --

  • 범주형 변수에 대한 기술통계 보기 - 1
df.describe(include="object")
  • 결측치의 합 구하기
df.isnull().sum()
  • 결측치의 평균 구하기
df.isnull().mean()

1. 범주형 데이터의 정리

(1) 기술통계 보기 - 2

df.describe(include="all")

frequency, top 값 등을 확인

(2) 범주형 데이터는 object 외에도 boolean 등의 형식으로도 구성.

(3) nunique 값 구하기

df.nunique()

(4) exclude로 특정 값 제외하고도 가능

2. pivot table

(1) 괄호의 의미

df.groupby("origin")["mpg"].mean()

mpg라는 컬럼으로 한정한다는 의미.

(2) pivot_table
aggfunc: AggFuncType='mean'
= 평균이 기본값이라는 의미

(3) barplot 으로 합계 값 구하기

sns.barplot(data=df, x="origin", y="mpg", hue="cylinders", estimator=sum)

estimator=sum이 해당 기능을 실행

(4) groupby 를 통해 시각화에 대한 값을 구하기

df.groupby(by=["origin","cylinders"])["mpg"].mean()

df.groupby("origin")["mpg"].describe()에서 소괄호, 중괄호가 각각 갖는 의미=origin 값 별로 mpg 값에 대한 통계를 구한다는 의미.
by라는 argument를 parameter로 넘김. 두 개의 값을 할당하지 않도록 유의.

(5) unstack
마지막 인덱스에 있는 값(=column 등)을 끌어올려서 볼 수 있음. (멀티인덱스와 무관하게)

(6) pd.pivot_table 을 통해 시각화에 대한 값을 구하기

pd.pivot_table(data=df, index="origin", columns="cylinders", values="mpg")

(6) pivot_table은 연산을 하고, pivot은 연산을 하지 않는다.

3. 그래프

(1) 막대그래프의 단점

  • 대표값만 표현된다.
  • 분포를 나타내주지 못한다.

(2) boxenplot
(3) scatterplot

sns.scatterplot(data=df, x="origin", y="mpg")

산점도는 빈도수 확인이 어렵다는 단점이 있다.
(4) stripplot
(5) swarmplot
(6) catplot

  • 서브플롯 구성 시, 다음과 같이 활용 가능
sns.catplot(data=df, x="origin", y="mpg", kind="violin", col="cylinders", col_wrap=3)

(7) countplot
유의점: countplot은 서브플롯 생성 시, x, y값 중 둘 중 한개만 할당해야 함

4. KRX 데이터 활용

(1) 로봇 배제 표준

  • 웹스크래핑 시, DDOS 공격으로 의심받을 수 있기 때문에 시간 차를 둔다.
time.sleep()

5. 네이버금융 데이터 활용

(1) url 가져오기

item_code = "035720"
item_name = "카카오"
page_no = 1
item_code = "https://finance.naver.com/item/main.naver?code=035720"
pd.read_html(url)

(2) read_html을 이용하여 데이터 가져오기

df = table[0]
df

table이라는 변수에 read_html에서 가져온 데이터 넣기
조건: 0번째 인덱스 값을 가진 데이터만 넣어주었음

(2) display로 출력

temp_list = []
for news in table:
    temp_list.append(news)
    display(news)

temp_list라는 변수에 가져온 테이블의 모든 내용들을 넣어줌

(3) 페이지컬럼 인덱싱

cols = df.columns
cols
temp_list = []
for news in table[:-1]:
    news.columns = cols
    temp_list.append(news)
    display(news)

page 제거

(4) 인덱스 번호 초기화

df_news.reset_index(drop=True)

(5) 비트와이즈 연산 (~)

  • 값을 반전시킨다.
    drop= True면 False가 되는 형식
    cf.)
    df_news["정보제공"].str.contains("연관기사") 부분에서 "연관기사"가 포함된 기사들의 데이터프레임을 구하고 이를 ~ 연산으로 이용해 반전


------ 모르는 점 ------
(1) groupby에서 by의 역할
(2) dropna 함수:
https://cosmosproject.tistory.com/308
(3) 얕은 복사와 깊은 복사
(4) f-string: https://www.daleseo.com/python-f-strings/

------ 퀴즈 오답노트 ------
(1) value_counts(): 하나의 변수에 대한 빈도수를 구한다.
(2) scatterplot() - 수치형 변수 표현 O, 범주형 변수 표현 X
(3) robots.txt는 검색엔진에게 수집해도 되는 페이지인지를 알려준다 (페이지, 에이전트 정보 등)

------ 논문 인사이트 ------

  1. Symbolic reasoning requires
    discovering the underlying rules of a data distribution rather
    than mimicking data patterns. - "Neural Sequence-to-grid Module for Learning Symbolic Rules"
profile
유저가 왜 그랬을까

0개의 댓글