0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has 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
#
#	xlsx_read.py
#
#					Jul/31/2024
# ------------------------------------------------------------------
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)
df.to_json("tmp001.json",orient='records')
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
Python 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> pd.__version__
'2.1.4'
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?