pandas.DataFrameのto_dict()の早見表

# 「df.to_dict()の出力ってどんなんだっけ...?」
と言うのを毎回調べている気がするので、ぱっと見でわかるようにしておくだけの記事です。

df ↓
<div class="jp-RenderedHTMLCommon jp-RenderedHTML jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/html">

  <div id="df-2276e4f2-6591-476b-a2cb-523696ab334d">
    <div class="colab-df-container">
      <div>
<style scoped="">
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>X</th>
      <th>Y</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <th>1</th>
      <td>3</td>
      <td>4</td>
    </tr>
  </tbody>
</table>

#### df.to_dict() \# df.to_dict(orient='dict')も同じ
</pre></div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-RenderedText jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/plain">
<pre>{'X': {0: 1, 1: 3}, 'Y': {0: 2, 1: 4}}</pre>

#### df.to_dict(orient='list')
</pre></div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-RenderedText jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/plain">
<pre>{'X': [1, 3], 'Y': [2, 4]}</pre>

#### df.to_dict(orient='series')
</pre></div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-RenderedText jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/plain">
<pre>{'X': 0    1
 1    3
 Name: X, dtype: int64,
 'Y': 0    2
 1    4
 Name: Y, dtype: int64}</pre>

#### df.to_dict(orient='split')
</pre></div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-RenderedText jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/plain">
<pre>{'index': [0, 1], 'columns': ['X', 'Y'], 'data': [[1, 2], [3, 4]]}</pre>

#### df.to_dict(orient='tight')
</pre></div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-RenderedText jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/plain">
<pre>{'index': [0, 1],
 'columns': ['X', 'Y'],
 'data': [[1, 2], [3, 4]],
 'index_names': [None],
 'column_names': [None]}</pre>

#### df.to_dict(orient='records')
</pre></div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-RenderedText jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/plain">
<pre>[{'X': 1, 'Y': 2}, {'X': 3, 'Y': 4}]</pre>

#### df.to_dict(orient='index')
</pre></div>
<div class="jp-Cell-outputWrapper">
<div class="jp-Collapser jp-OutputCollapser jp-Cell-outputCollapser">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-RenderedText jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/plain">
<pre>{0: {'X': 1, 'Y': 2}, 1: {'X': 3, 'Y': 4}}</pre>