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

More than 1 year has passed since last update.

pandasでJSONファイルを読み込む&CSVに書き出す方法メモ

Last updated at Posted at 2023-02-19

概要

とあるJSONファイルのデータを加工しようと思い、pandasで読み込もうとしたけどいろいろとミスって時間がかかってしまったので、忘備録としてここにうまくいった方法をメモっときます。

サンプルJSONファイル

sample.json
{
	"tanaka": {"id": 123, "age": 30},
	"sizuki": {"id": 456,"age": 24},
	"satou": {"id": 789, "age": 47}
}

手順

1.pandasライブラリ、jsonモジュールのインポート

import pandas as pd
import json

2.ファイル読み込み

data = open('sample.json')

3.pd.read_json()でDataFrameとしてデータ読み込み

df = pd.read_json(data)

4.print(df)でデータ確認

print(df)

#こんな感じの表が表示されるはず

#     tanaka  sizuki  satou
#id      123     456    789
#age      30      24     47

5.to_csv()でCSVファイルに書き出す

df.to_csv('sample.csv')

以上です。

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