pandas:Excelファイルを読み込んでDataFrameを作成する方法

スポンサーリンク

Excelファイルを読み込んでDataFrameを作成する方法

Excelファイルを読み込んでDataFrameを作成するにはread_excelを使用します。

test.xlsxのデータ

id	name	count	rating
1	abc	13	4.38
2	defgh	4	8.56
3	ij	0	1.25
4	klmnopq	23	3.49
5	rst	11	0.51
import pandas as pd

df = pd.read_excel("test.xlsx")
print(df)
#    id     name  count  rating
# 0   1      abc     13    4.38
# 1   2    defgh      4    8.56
# 2   3       ij      0    1.25
# 3   4  klmnopq     23    3.49
# 4   5      rst     11    0.51

xlsxファイルを読み込もうとして以下のようなエラーメッセージが表示された場合は、

ValueError: Your version of xlrd is 2.0.1. In xlrd >= 2.0, only the xls format is supported. Install openpyxl instead.

openpyxlをインストールします。

$ pip install openpyxl