LoginSignup
10
12

More than 5 years have passed since last update.

Pandas を用いて複数シートからなるエクセルファイルをデータフレームに取り込む

Last updated at Posted at 2018-11-28
以下のような2枚のシートからなるエクセルファイルをデータフレームに取り込む場合
Excelsampledata.xls[sheet1]

sheet1.png

Excelsampledata.xls[sheet2]

sheet2.png

サンプルプログラム
sample.py
import pandas as pd

#エクセルファイルを読み込み
input_file = pd.ExcelFile("Excelsampledata.xls")
#シート名を格納
input_sheet_name=input_file.sheet_names
#シートの枚数を確認
num_sheet=len(input_sheet_name)

#de_listというリストを作成し、エクセルのシートをリストに格納していく。
df_list=[]
for sheet in input_sheet_name:
    df_list.append(input_file.parse(sheet))

#カラムを並び替えないで、読み込んだエクセルのシートを連結し、データフレームに格納する。
df=pd.DataFrame()
for i in range(num_sheet) :
    df=df.append(df_list[i])[df_list[0].columns.tolist()]

#欠損値を削除する。
df=df.dropna()
結果(データフレーム(df))

result.png

10
12
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
10
12