python pandas 라이브러리를 이용한 엑셀(excel)파일 읽어들이기.

python pandas 라이브러리를 이용한 엑셀(excel)파일 읽어들이기.

1. python Pandas 라이브러리 설치하기

   : pip install pandas
pip install xlrd

2. 샘플 엑셀파일 받기   

movies.xls를 열면 이런식으로 되어있음
3. 소스보고 따라해보기

(위에 다운받은 파일을 넣은 폴더안에 임의의 python 파일을 만들어서 붙여넣는다)

import pandas as pd

excel_file = 'movies.xls'
movies = pd.read_excel(excel_file)
print(movies.head())
print(movies.shape)

print('---------------------------------------------------------------')
#sheet1 을 읽어온다.
movies_sheet1 = pd.read_excel(excel_file, sheet_name=0, index_col=0)
print(movies_sheet1.head())
print(movies_sheet1.shape)

print('---------------------------------------------------------------')
#sheet2 을 읽어온다
movies_sheet2 = pd.read_excel(excel_file, sheet_name=1, index_col=0)
print(movies_sheet2.head())
print(movies_sheet2.shape)

print('---------------------------------------------------------------')
#sheet3 을 읽어온다.
movies_sheet3 = pd.read_excel(excel_file, sheet_name=2, index_col=0)
print(movies_sheet3.head())
print(movies_sheet3.shape)

print('--------------------------------------------------------------')
#위의 sheet1, 2, 3 을 모두 합친다.
movies = pd.concat([movies_sheet1, movies_sheet2, movies_sheet3])
print(movies.shape)

————————————  실행결과 --------------------------------------------------

D:\ProgramData\miniconda3\python.exe D:/02_Workspace/python/ptest/movie.py
                                               Title  ...  IMDB Score
0  Intolerance: Love's Struggle Throughout the Ages   ...         8.0
1                    Over the Hill to the Poorhouse   ...         4.8
2                                    The Big Parade   ...         8.3
3                                        Metropolis   ...         8.3
4                                     Pandora's Box   ...         8.0

[5 rows x 25 columns]
(1338, 25)
---------------------------------------------------------------
                                                   Year  ... IMDB Score
Title                                                    ...           
Intolerance: Love's Struggle Throughout the Ages   1916  ...        8.0
Over the Hill to the Poorhouse                     1920  ...        4.8
The Big Parade                                     1925  ...        8.3
Metropolis                                         1927  ...        8.3
Pandora's Box                                      1929  ...        8.0

[5 rows x 24 columns]
(1338, 24)
---------------------------------------------------------------
                        Year  ... IMDB Score
Title                         ...           
102 Dalmatians          2000  ...        4.8
28 Days                 2000  ...        6.0
3 Strikes               2000  ...        4.0
Aberdeen                2000  ...        7.3
All the Pretty Horses   2000  ...        5.8

[5 rows x 24 columns]
(2100, 24)
---------------------------------------------------------------
                                        Year  ... IMDB Score
Title                                         ...           
127 Hours                             2010.0  ...        7.6
3 Backyards                           2010.0  ...        5.2
3                                     2010.0  ...        6.8
8: The Mormon Proposition             2010.0  ...        7.1
A Turtle's Tale: Sammy's Adventures   2010.0  ...        6.1

[5 rows x 24 columns]
(1604, 24)
--------------------------------------------------------------
(5042, 24)

Process finished with exit code 0

 

만족하셨나요? ~~~~~~~

#pandas #Python Pandas #Python Excel 읽기

Image 3
이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
Image 4
이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.