250x250
Link
λ‚˜μ˜ GitHub Contribution κ·Έλž˜ν”„
Loading data ...
Notice
Recent Posts
Recent Comments
관리 메뉴

Data Science LAB

[Python]Tabular Data μ›Ήμ—μ„œ 크둀링 λ³Έλ¬Έ

🐍 Python/Crawling

[Python]Tabular Data μ›Ήμ—μ„œ 크둀링

γ…… γ…œ γ…” γ…‡ 2022. 2. 13. 00:00
728x90
Tabular Data λž€? 

- μ—‘μ…€ 파일 ν˜•μ‹μ΄λ‚˜ κ΄€κ³„ν˜• λ°μ΄ν„°λ² μ΄μŠ€μ˜ ν…Œμ΄λΈ”μ— 담을 수 μžˆλŠ” 데이터

- 즉, ν–‰κ³Ό μ—΄λ‘œ ν‘œν˜„μ΄ κ°€λŠ₯ν•œ 데이터

 

즉, 데이터 λΆ„μ„μ—μ„œ 자주 μ‚¬μš©ν•˜λŠ” ν‘œ ν˜•μ‹μ˜ 데이터이닀. 

 

 

Tabular Data μ›Ήμ—μ„œ scrapping

였늘 뢈러올 λ°μ΄ν„°λŠ” νŽœμ‹€λ² λ‹ˆμ•„ 데이터이닀. 

https://en.wikipedia.org/wiki/Politics_of_Pennsylvania

 

 

1. ν•„μš”ν•œ 라이브러리 뢈러였기

import pandas as pd
import numpy as np

 

2. 데이터 λ‘œλ”©

table_PA = pd.read_html('http://en.wikipedia.org/wiki/Politics_of_Pennsylvania')
len(table_PA)

뢈러온 데이터셋을 확인해 보면 μ›ΉνŽ˜μ΄μ§€μ˜ λͺ¨λ“  뢀뢄이 크둀링 된 것을 확인할 수 μžˆλ‹€. 

λ”°λΌμ„œ 뢈러였고자 ν•˜λŠ” tabular dataλ§Œμ„ μ§€μ •ν•΄μ„œ λ‹€μ‹œ λΆˆλŸ¬μ™€μ•Ό ν•œλ‹€. 

 

 

3. ν•„μš”ν•œ 데이터 λ‹€μ‹œ λ‘œλ”©

table_PA = pd.read_html('https://en.wikipedia.org/wiki/Politics_of_Pennsylvania',match='Presidential election results')
len(table_PA)

데이터셋을 λΆˆλŸ¬μ˜€λŠ” μ½”λ“œμ— matchλ₯Ό μΆ”κ°€ν•˜λ©΄ μ›ν•˜λŠ” λ°μ΄ν„°μ…‹λ§Œμ„ 뢈러올 수 μžˆλ‹€. 

μ‹€ν–‰ κ²°κ³Ό ν…Œμ΄λΈ” μˆ˜κ°€ 1개둜 잘 λΆˆλŸ¬μ™€μ§„ 것을 확인할 수 μžˆλ‹€. 

 

 

4. 데이터 정보 확인

df = table_PA[0]
df.head()

λ°μ΄ν„°μ…‹μ˜ μœ„μ˜ 5개 λ°μ΄ν„°λ§Œ 확인해 λ³΄λ‹ˆ %둜 κ΅¬μ„±λœ 컬럼이 μžˆλŠ” 것을 확인

 

 

5. 데이터 λ³€ν™˜

df['Democratic'] = df['Democratic'].str[:4]
df['Republican'] = df['Republican'].str[:4]
df.head()

df.info()

'Democratic','Rebublican'컬럼이 objectν˜•μ‹μΈ 것을 확인

 

df[['Democratic','Republican']] = df[['Democratic','Republican']].apply(pd.to_numeric)
df.info()

 

 

이 μ½”λ“œλ₯Ό 기반으둜 μ›Ήμ—μ„œ λ‹€μ–‘ν•œ 데이터λ₯Ό 크둀링 ν•΄μ˜€λŠ” μ—°μŠ΅μ„ 더 ν•΄μ•Όν•  것 κ°™λ‹€ γ…Žγ…Ž

 

728x90
Comments