2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Excelファイルの複数シートの読込(Python Pandas VS Power Query)

2
Last updated at Posted at 2026-08-10

はじめに

前回は以下記事を書きました。

今回は、PythonでのExcelファイルの全シート読込です。
前回同様、Power QueryのM式と比較してアウトプットしたいと思います。

Python Pandasで読込む場合

Pandasのread_excel関数を使います。
sheet_name=Noneにすることで全シートを読み込めます。

import pandas as pd

df = pd.read_excel(
    r'Data\student_3sheets_5cols_100rows.xlsx',
    sheet_name=None
)

df_all = pd.concat(df.values(), ignore_index=True)

print(df_all.shape)
display(df_all.head())

この場合、dfはデータフレームではなくデータフレームを持つ辞書になります。
dfはキーにシート名、バリューにデータフレームを持ちます。

df = {
    'シート1': DataFrame,
    'シート2': DataFrame,
    'シート3': DataFrame
}

concat関数で各データフレームを縦方向に連結します。
ignore_index=Trueは連結後に行番号を振りなおします。

Power Queryで読込む場合

Excel.Workbook関数を使います。

  • 2つ目の引数をtrueにすると先頭行をヘッダーにします
  • 3つ目の引数をtrueにすると型指定しなくなります

Table.SelectColumns関数でData列以外を削除します。
Table.ExpandTableColumn関数でテーブルを開きます。

let
    ソース = Excel.Workbook(
        File.Contents("C:Python\データ収集\Data\student_3sheets_5cols_100rows.xlsx"), true, true),
        削除された他の列 = Table.SelectColumns(ソース,{"Data"}),
        #"展開された Data" = Table.ExpandTableColumn(削除された他の列, "Data", {"学籍番号", "テスト点数", "出席率(%)", "クラス", "受験日"})
in
    #"展開された Data"

次回の内容

今度は複数のファイルの読込をPython PandasとPower Queryで試して記事にしたいと思います。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?