Pandas: データフレームについて--09: データフレームを表示する

# データフレームの表示


```python
import pandas as pd
import numpy as np

n = 65
np.random.seed(123)
df = pd.DataFrame({
    'a': np.arange(n) + 100,
    'b': np.random.rand(n),
    'c': np.around(np.random.normal(50, 10, n)),
    'd': np.random.choice(['A', 'B', 'C', 'D', 'E'], n)
})
df
```




<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>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>100</td>
      <td>0.696469</td>
      <td>51.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>101</td>
      <td>0.286139</td>
      <td>48.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>2</th>
      <td>102</td>
      <td>0.226851</td>
      <td>43.0</td>
      <td>E</td>
    </tr>
    <tr>
      <th>3</th>
      <td>103</td>
      <td>0.551315</td>
      <td>44.0</td>
      <td>B</td>
    </tr>
    <tr>
      <th>4</th>
      <td>104</td>
      <td>0.719469</td>
      <td>41.0</td>
      <td>C</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>60</th>
      <td>160</td>
      <td>0.669314</td>
      <td>49.0</td>
      <td>E</td>
    </tr>
    <tr>
      <th>61</th>
      <td>161</td>
      <td>0.585937</td>
      <td>42.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>62</th>
      <td>162</td>
      <td>0.624904</td>
      <td>48.0</td>
      <td>B</td>
    </tr>
    <tr>
      <th>63</th>
      <td>163</td>
      <td>0.674689</td>
      <td>36.0</td>
      <td>C</td>
    </tr>
    <tr>
      <th>64</th>
      <td>164</td>
      <td>0.842342</td>
      <td>41.0</td>
      <td>C</td>
    </tr>
  </tbody>
</table>
<p>65 rows × 4 columns</p>
</div>



## 1.　最初の n 行を表示

    head(n: 'int' = 5)


```python
df.head()
```




<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>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>100</td>
      <td>0.696469</td>
      <td>51.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>101</td>
      <td>0.286139</td>
      <td>48.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>2</th>
      <td>102</td>
      <td>0.226851</td>
      <td>43.0</td>
      <td>E</td>
    </tr>
    <tr>
      <th>3</th>
      <td>103</td>
      <td>0.551315</td>
      <td>44.0</td>
      <td>B</td>
    </tr>
    <tr>
      <th>4</th>
      <td>104</td>
      <td>0.719469</td>
      <td>41.0</td>
      <td>C</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.head(2)
```




<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>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>100</td>
      <td>0.696469</td>
      <td>51.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>101</td>
      <td>0.286139</td>
      <td>48.0</td>
      <td>A</td>
    </tr>
  </tbody>
</table>
</div>



## 2.　最後の n 行を表示

    tail(n: 'int' = 5)


```python
df.tail()
```




<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>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>60</th>
      <td>160</td>
      <td>0.669314</td>
      <td>49.0</td>
      <td>E</td>
    </tr>
    <tr>
      <th>61</th>
      <td>161</td>
      <td>0.585937</td>
      <td>42.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>62</th>
      <td>162</td>
      <td>0.624904</td>
      <td>48.0</td>
      <td>B</td>
    </tr>
    <tr>
      <th>63</th>
      <td>163</td>
      <td>0.674689</td>
      <td>36.0</td>
      <td>C</td>
    </tr>
    <tr>
      <th>64</th>
      <td>164</td>
      <td>0.842342</td>
      <td>41.0</td>
      <td>C</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.tail(2)
```




<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>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>63</th>
      <td>163</td>
      <td>0.674689</td>
      <td>36.0</td>
      <td>C</td>
    </tr>
    <tr>
      <th>64</th>
      <td>164</td>
      <td>0.842342</td>
      <td>41.0</td>
      <td>C</td>
    </tr>
  </tbody>
</table>
</div>



## 3.　任意の行を表示

以下の方法以外は 「データフレームについて--03: 行，列の取り出し」 を参照のこと。

https://qiita.com/WolfMoon/items/5bdb05d67d372d70b750

```python
df.iloc[8:12, :]
```




<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>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>8</th>
      <td>108</td>
      <td>0.480932</td>
      <td>63.0</td>
      <td>D</td>
    </tr>
    <tr>
      <th>9</th>
      <td>109</td>
      <td>0.392118</td>
      <td>71.0</td>
      <td>E</td>
    </tr>
    <tr>
      <th>10</th>
      <td>110</td>
      <td>0.343178</td>
      <td>48.0</td>
      <td>C</td>
    </tr>
    <tr>
      <th>11</th>
      <td>111</td>
      <td>0.729050</td>
      <td>48.0</td>
      <td>C</td>
    </tr>
  </tbody>
</table>
</div>



以下のような場合，`[1, 3, 5, 7:10]` と指定したくなるが，それではエラーになる。


```python
df.iloc[[1, 3, 5, 7, 8, 9, 10], :]
```




<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>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>101</td>
      <td>0.286139</td>
      <td>48.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>3</th>
      <td>103</td>
      <td>0.551315</td>
      <td>44.0</td>
      <td>B</td>
    </tr>
    <tr>
      <th>5</th>
      <td>105</td>
      <td>0.423106</td>
      <td>52.0</td>
      <td>B</td>
    </tr>
    <tr>
      <th>7</th>
      <td>107</td>
      <td>0.684830</td>
      <td>59.0</td>
      <td>B</td>
    </tr>
    <tr>
      <th>8</th>
      <td>108</td>
      <td>0.480932</td>
      <td>63.0</td>
      <td>D</td>
    </tr>
    <tr>
      <th>9</th>
      <td>109</td>
      <td>0.392118</td>
      <td>71.0</td>
      <td>E</td>
    </tr>
    <tr>
      <th>10</th>
      <td>110</td>
      <td>0.343178</td>
      <td>48.0</td>
      <td>C</td>
    </tr>
  </tbody>
</table>
</div>



## 4.　全部の行を表示させる

冒頭に示したように，60 行を超える場合は，デフォルトで省略表示される。

無理やり全行表示させるには，オプションを設定する。

    set_option(pat, value)

`'display.max_rows'` に `None` を設定すればよい。当然，数値で希望する値を設定することもできる。


```python
pd.set_option('display.max_rows', None)
df
```




<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>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>100</td>
      <td>0.696469</td>
      <td>51.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>1</th>
      <td>101</td>
      <td>0.286139</td>
      <td>48.0</td>
      <td>A</td>
    </tr>
    <tr>
      <th>2</th>
      <td>102</td>
      <td>0.226851</td>
      <td>43.0</td>
      <td>E</td>
    </tr>
    <tr><td></td><td>以下略</td><td></td><td></td><td></td></tr>
  </tbody>
</table>
</div>



## 5.　もっと知りたい人

    help(pd.set_option) # Help on CallableDynamicDoc in module pandas._config.config
    help(df.first)      # Help on method first in module pandas.core.generic
    help(df.last)       # Help on method last in module pandas.core.generic
    help(df.iloc)       # Help on _iLocIndexer in module pandas.core.indexing object
    help(df.loc)        # Help on _LocIndexer in module pandas.core.indexing object