LoginSignup
42
68

More than 5 years have passed since last update.

【Python】pandasを使ってExcelを読み込みたい

Last updated at Posted at 2016-07-06

データ分析でExcelファイルを読み込むことが意外と多いので。

とりあえずxlrdをインストールしておかないといけないみたい。

pip install xlrd

個人的にpandasで読み込むのが一番簡単だったのでpandasを使う。

import pandas as pd

複数ブック/複数シートを読み込んで,各シートをDataFrameにしてリスト化する。

books = ['book1.xlsx', 'book2.xlsx']
df_list = []

for book in books:
    file = pd.ExcelFile(book) # bookを読む
    for sheet in file.sheet_names:
        df_list.append(file.parse(sheet)) # シートを順々にデータフレーム化

ここまでくれば後は焼くなり煮るなり。
pandas.ExcelFile.parseで比較的よく使う引数は以下。

  • skiprows: 最初の何行をスキップするか
  • header: 最初の行をヘッダーとして読み込まない場合はheader=None
42
68
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
42
68