반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import pandas as pd
###웹에서 가져오기###
#HTML 웹 페이지에서 표 가져오기#
#read_html() 메소드 사용, html 웹페이지에 있는 표를 데이터프레임으로 변환한 후 리스트의 형태로 반환한다#
#pip install lxml# #터미널에서 lxml을 설치해야 한다#
html = pd.read_html('C:/Users/ZenBook/Desktop/code/sample/part2/sample.html')
print(html)
print(len(html)) ##sample.html 안에 표가 2개이므로 2가 출력된다.
for i in range(len(html)): ##for문을 사용해서 각각 테이블마다 나눠서 출력한다.
print("Table[%s]" %i) ##html 웹 페이지를 가져올때 표마다 한 개의 데이터프레임으로 변환하고 그것을 모아서
print(html[i]) ##시리즈로 반환하기 때문에 시리즈[i]로 각각의 표에 접근이 가능하다.
print('\n')
'''
Table[0]
Unnamed: 0 c0 c1 c2 c3
0 0 0 1 4 7
1 1 1 2 5 8
2 2 2 3 6 9
Table[1]
name year developer opensource
0 NumPy 2006 Travis Oliphant True
1 matplotlib 2003 John D. Hunter True
2 pandas 2008 Wes Mckinneye True
'''
df = html[1] ##하나의 데이터프레임(표)만 가져와서 사용할 수 있다.
df.set_index(['name'], inplace=True) ##'name'열을 행 인덱스로 사용한다.
print(df)
'''
year developer opensource
name
NumPy 2006 Travis Oliphant True
matplotlib 2003 John D. Hunter True
pandas 2008 Wes Mckinneye True
'''
|
cs |
반응형
'Pandas' 카테고리의 다른 글
[Pandas]데이터 구조, 분석 (0) | 2022.11.03 |
---|---|
[Pandas]데이터 저장하기 (0) | 2022.11.03 |
[Pandas]외부 파일 불러오기 (0) | 2022.11.02 |
[Pandas]데이터프레임 연산 (0) | 2022.11.02 |
[Pandas]시리즈 연산 (1) | 2022.11.02 |