LoginSignup
0
2

More than 3 years have passed since last update.

pandas で Excelファイル(*.xlsx) の読み書き

Last updated at Posted at 2018-10-01

xlsx の作成

xlsx_create.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   xlsx_create.py
#
#                   Oct/01/2018
# ------------------------------------------------------------------
import sys
import pandas as pd
#
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
df = pd.DataFrame([
        ["t1271","千葉",59476,"2003-9-21"],
        ["t1272","勝浦",49573,"2003-3-14"],
        ["t1273","市原",29471,"2003-6-25"],
        ["t1274","流山",39872,"2003-8-28"],
        ["t1275","八千代",27176,"2003-10-5"],
        ["t1276","我孫子",13175,"2003-1-15"],
        ["t1277","鴨川",85134,"2003-12-21"]])

print(df)
#
path="./cities.xlsx"
#
df.to_excel(path,header=False,index=False)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

xlsx の読み込み

xlsx_read.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   xlsx_read.py
#
#                   Oct/01/2018
# ------------------------------------------------------------------
import sys
import pandas as pd
#
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
file_xlsx = "./cities.xlsx"
#
df=pd.read_excel(file_xlsx,header=None)
print(df)
print(df.head())
#
df.to_csv("tmp001.csv",header=None,index=None)
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./xlsx_read.py
*** 開始 ***
       0    1      2           3
0  t1271   千葉  59476   2003-9-21
1  t1272   勝浦  49573   2003-3-14
2  t1273   市原  29471   2003-6-25
3  t1274   流山  39872   2003-8-28
4  t1275  八千代  27176   2003-10-5
5  t1276  我孫子  13175   2003-1-15
6  t1277   鴨川  85134  2003-12-21
       0    1      2          3
0  t1271   千葉  59476  2003-9-21
1  t1272   勝浦  49573  2003-3-14
2  t1273   市原  29471  2003-6-25
3  t1274   流山  39872  2003-8-28
4  t1275  八千代  27176  2003-10-5
*** 終了 ***

次のバージョンで確認しました。

$ python --version
Python 3.9.1

pandas

>>> import pandas as pd
>>> print(pd.__version__)
1.2.1
0
2
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
0
2