6
5

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.DataFrameのto_dict()の早見表

Posted at

「df.to_dict()の出力ってどんなんだっけ...?」

と言うのを毎回調べている気がするので、ぱっと見でわかるようにしておくだけの記事です。

df ↓

X Y
0 1 2
1 3 4

df.to_dict() # df.to_dict(orient='dict')も同じ

{'X': {0: 1, 1: 3}, 'Y': {0: 2, 1: 4}}

df.to_dict(orient='list')

{'X': [1, 3], 'Y': [2, 4]}

df.to_dict(orient='series')

{'X': 0    1
 1    3
 Name: X, dtype: int64,
 'Y': 0    2
 1    4
 Name: Y, dtype: int64}

df.to_dict(orient='split')

{'index': [0, 1], 'columns': ['X', 'Y'], 'data': [[1, 2], [3, 4]]}

df.to_dict(orient='tight')

{'index': [0, 1],
 'columns': ['X', 'Y'],
 'data': [[1, 2], [3, 4]],
 'index_names': [None],
 'column_names': [None]}

df.to_dict(orient='records')

[{'X': 1, 'Y': 2}, {'X': 3, 'Y': 4}]

df.to_dict(orient='index')

{0: {'X': 1, 'Y': 2}, 1: {'X': 3, 'Y': 4}}
6
5
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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?