pandas DataFrameの出力行を絞るheadとtailを同時に使う


pandas使ってデータの中身を見るとき`df`と打つとダラダラっと長い出力が出てきて見た目がよろしくありません。


```python
df = pd.DataFrame(np.random.randn(100,4)); df
```




<div>
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.374859</td>
      <td>0.327898</td>
      <td>2.215511</td>
      <td>1.165490</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-0.939833</td>
      <td>-0.531873</td>
      <td>-1.717368</td>
      <td>-0.584834</td>
    </tr>
    <tr>
      <th>2</th>
      <td>0.759525</td>
      <td>1.992222</td>
      <td>-0.352082</td>
      <td>-1.500736</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.279484</td>
      <td>-0.278289</td>
      <td>0.625053</td>
      <td>0.362855</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1.151177</td>
      <td>-1.398746</td>
      <td>0.391291</td>
      <td>0.673220</td>
    </tr>
    <tr>
      <th>5</th>
      <td>-0.392235</td>
      <td>-0.973586</td>
      <td>0.243700</td>
      <td>-2.899188</td>
    </tr>
    <tr>
      <th>6</th>
      <td>0.837239</td>
      <td>0.670279</td>
      <td>-0.692629</td>
      <td>-1.126292</td>
    </tr>
    <tr>
      <th>7</th>
      <td>-0.921781</td>
      <td>-2.438753</td>
      <td>-0.519993</td>
      <td>0.482150</td>
    </tr>
    <tr>
      <th>8</th>
      <td>2.459798</td>
      <td>1.219577</td>
      <td>0.770672</td>
      <td>-1.390487</td>
    </tr>
    <tr>
      <th>9</th>
      <td>-1.093845</td>
      <td>0.343168</td>
      <td>-0.229751</td>
      <td>1.172888</td>
    </tr>
    <tr>
      <th>10</th>
      <td>-0.437252</td>
      <td>-0.824611</td>
      <td>-0.346145</td>
      <td>0.785992</td>
    </tr>
    <tr>
      <th>11</th>
      <td>1.193672</td>
      <td>-0.193474</td>
      <td>0.676684</td>
      <td>-1.468454</td>
    </tr>
    <tr>
      <th>12</th>
      <td>1.039551</td>
      <td>0.234592</td>
      <td>-0.192957</td>
      <td>-1.210177</td>
    </tr>
    <tr>
      <th>13</th>
      <td>1.081615</td>
      <td>-0.988146</td>
      <td>-0.021931</td>
      <td>1.137428</td>
    </tr>
    <tr>
      <th>14</th>
      <td>0.470213</td>
      <td>1.239319</td>
      <td>-0.346861</td>
      <td>-0.288200</td>
    </tr>
    <tr>
      <th>15</th>
      <td>-0.339914</td>
      <td>-1.580660</td>
      <td>-0.432387</td>
      <td>-0.202277</td>
    </tr>
    <tr>
      <th>16</th>
      <td>1.141389</td>
      <td>0.236465</td>
      <td>-1.477666</td>
      <td>-0.264886</td>
    </tr>
    <tr>
      <th>17</th>
      <td>-0.686339</td>
      <td>0.971620</td>
      <td>-0.733747</td>
      <td>-0.110410</td>
    </tr>
    <tr>
      <th>18</th>
      <td>0.266442</td>
      <td>-0.168084</td>
      <td>-2.021432</td>
      <td>-1.337447</td>
    </tr>
    <tr>
      <th>19</th>
      <td>0.698942</td>
      <td>1.409780</td>
      <td>-0.506928</td>
      <td>0.999617</td>
    </tr>
    <tr>
      <th>20</th>
      <td>0.432697</td>
      <td>-0.629534</td>
      <td>-0.605271</td>
      <td>-2.336144</td>
    </tr>
    <tr>
      <th>21</th>
      <td>1.377673</td>
      <td>0.761185</td>
      <td>1.023692</td>
      <td>-1.472238</td>
    </tr>
    <tr>
      <th>22</th>
      <td>0.152084</td>
      <td>-0.725003</td>
      <td>1.553365</td>
      <td>-0.544019</td>
    </tr>
    <tr>
      <th>23</th>
      <td>0.156944</td>
      <td>0.505415</td>
      <td>-1.222674</td>
      <td>0.423808</td>
    </tr>
    <tr>
      <th>24</th>
      <td>0.479288</td>
      <td>0.201019</td>
      <td>-0.091332</td>
      <td>0.254680</td>
    </tr>
    <tr>
      <th>25</th>
      <td>-1.184456</td>
      <td>-0.095066</td>
      <td>-0.885104</td>
      <td>0.421549</td>
    </tr>
    <tr>
      <th>26</th>
      <td>1.040014</td>
      <td>-1.381022</td>
      <td>1.869261</td>
      <td>1.437337</td>
    </tr>
    <tr>
      <th>27</th>
      <td>0.478984</td>
      <td>-0.944046</td>
      <td>0.352453</td>
      <td>2.569114</td>
    </tr>
    <tr>
      <th>28</th>
      <td>-0.439603</td>
      <td>-1.298592</td>
      <td>0.913691</td>
      <td>-0.622890</td>
    </tr>
    <tr>
      <th>29</th>
      <td>-0.545850</td>
      <td>-0.872281</td>
      <td>0.213367</td>
      <td>-0.681539</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>70</th>
      <td>-1.469679</td>
      <td>-0.456337</td>
      <td>-0.329848</td>
      <td>0.286484</td>
    </tr>
    <tr>
      <th>71</th>
      <td>-1.219977</td>
      <td>-2.282746</td>
      <td>0.506492</td>
      <td>-0.200502</td>
    </tr>
    <tr>
      <th>72</th>
      <td>0.365171</td>
      <td>0.926229</td>
      <td>0.935084</td>
      <td>-1.001133</td>
    </tr>
    <tr>
      <th>73</th>
      <td>-0.842211</td>
      <td>-0.040298</td>
      <td>-0.728098</td>
      <td>2.851352</td>
    </tr>
    <tr>
      <th>74</th>
      <td>-0.897560</td>
      <td>0.861064</td>
      <td>1.990610</td>
      <td>0.267552</td>
    </tr>
    <tr>
      <th>75</th>
      <td>-0.703071</td>
      <td>0.784476</td>
      <td>-1.002520</td>
      <td>-0.450417</td>
    </tr>
    <tr>
      <th>76</th>
      <td>0.033787</td>
      <td>0.073530</td>
      <td>0.214343</td>
      <td>1.787105</td>
    </tr>
    <tr>
      <th>77</th>
      <td>-1.258251</td>
      <td>-0.030197</td>
      <td>1.320570</td>
      <td>0.393222</td>
    </tr>
    <tr>
      <th>78</th>
      <td>-1.766407</td>
      <td>-0.996086</td>
      <td>-0.192385</td>
      <td>-0.513102</td>
    </tr>
    <tr>
      <th>79</th>
      <td>-0.629811</td>
      <td>-0.487538</td>
      <td>0.923048</td>
      <td>-1.497247</td>
    </tr>
    <tr>
      <th>80</th>
      <td>0.083737</td>
      <td>0.317975</td>
      <td>0.325503</td>
      <td>0.372319</td>
    </tr>
    <tr>
      <th>81</th>
      <td>-0.391032</td>
      <td>-1.192947</td>
      <td>0.312277</td>
      <td>-0.249235</td>
    </tr>
    <tr>
      <th>82</th>
      <td>-1.525711</td>
      <td>-0.994144</td>
      <td>-1.411683</td>
      <td>-0.297697</td>
    </tr>
    <tr>
      <th>83</th>
      <td>-0.794180</td>
      <td>0.776143</td>
      <td>0.057774</td>
      <td>1.659901</td>
    </tr>
    <tr>
      <th>84</th>
      <td>-0.270637</td>
      <td>-1.165053</td>
      <td>0.508089</td>
      <td>-0.445596</td>
    </tr>
    <tr>
      <th>85</th>
      <td>-1.961543</td>
      <td>1.973141</td>
      <td>0.533462</td>
      <td>-1.327931</td>
    </tr>
    <tr>
      <th>86</th>
      <td>-0.100805</td>
      <td>-0.162729</td>
      <td>1.448156</td>
      <td>-0.224008</td>
    </tr>
    <tr>
      <th>87</th>
      <td>-0.514309</td>
      <td>0.323078</td>
      <td>-0.233127</td>
      <td>1.384196</td>
    </tr>
    <tr>
      <th>88</th>
      <td>-2.516856</td>
      <td>0.374363</td>
      <td>1.129207</td>
      <td>1.069754</td>
    </tr>
    <tr>
      <th>89</th>
      <td>0.577997</td>
      <td>-0.767833</td>
      <td>0.923292</td>
      <td>-0.311372</td>
    </tr>
    <tr>
      <th>90</th>
      <td>0.758016</td>
      <td>-0.920520</td>
      <td>0.109853</td>
      <td>0.021920</td>
    </tr>
    <tr>
      <th>91</th>
      <td>0.406649</td>
      <td>-0.239311</td>
      <td>1.024492</td>
      <td>1.009525</td>
    </tr>
    <tr>
      <th>92</th>
      <td>-1.666999</td>
      <td>1.912280</td>
      <td>-1.959626</td>
      <td>-1.008634</td>
    </tr>
    <tr>
      <th>93</th>
      <td>0.222210</td>
      <td>-1.378929</td>
      <td>-0.609868</td>
      <td>0.749869</td>
    </tr>
    <tr>
      <th>94</th>
      <td>-1.622319</td>
      <td>0.035508</td>
      <td>-1.547729</td>
      <td>-0.480135</td>
    </tr>
    <tr>
      <th>95</th>
      <td>0.895873</td>
      <td>-0.045676</td>
      <td>0.180615</td>
      <td>-1.252418</td>
    </tr>
    <tr>
      <th>96</th>
      <td>-0.630758</td>
      <td>-0.285296</td>
      <td>0.160133</td>
      <td>1.106705</td>
    </tr>
    <tr>
      <th>97</th>
      <td>-1.909217</td>
      <td>0.841634</td>
      <td>-0.011388</td>
      <td>0.348177</td>
    </tr>
    <tr>
      <th>98</th>
      <td>-1.271435</td>
      <td>1.725388</td>
      <td>1.075685</td>
      <td>-0.164461</td>
    </tr>
    <tr>
      <th>99</th>
      <td>0.379877</td>
      <td>-2.547350</td>
      <td>0.899402</td>
      <td>-1.615333</td>
    </tr>
  </tbody>
</table>
<p>100 rows × 4 columns</p>
</div>



# pandasのオプションを変更

出力列の最大値は`set_option`メソッドで変更できます。

参考: [pandas 0.21.1 documentation](https://pandas.pydata.org/pandas-docs/stable/options.html)


```python
pd.set_option("display.max_rows", 10)
df
```




<div>
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.044887</td>
      <td>1.109229</td>
      <td>-1.404712</td>
      <td>0.014551</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-2.032691</td>
      <td>-0.435130</td>
      <td>-0.428953</td>
      <td>-0.537191</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.777178</td>
      <td>-0.435460</td>
      <td>0.848413</td>
      <td>-1.635667</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.213586</td>
      <td>-1.509976</td>
      <td>-0.635302</td>
      <td>-0.138209</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0.639734</td>
      <td>0.446097</td>
      <td>-1.312515</td>
      <td>0.783796</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>95</th>
      <td>-0.288322</td>
      <td>0.132045</td>
      <td>-0.405144</td>
      <td>-0.542612</td>
    </tr>
    <tr>
      <th>96</th>
      <td>-0.631888</td>
      <td>-0.487683</td>
      <td>0.684156</td>
      <td>-0.114015</td>
    </tr>
    <tr>
      <th>97</th>
      <td>-0.405025</td>
      <td>1.719596</td>
      <td>0.451788</td>
      <td>0.930674</td>
    </tr>
    <tr>
      <th>98</th>
      <td>0.146326</td>
      <td>1.032291</td>
      <td>-0.474848</td>
      <td>0.847260</td>
    </tr>
    <tr>
      <th>99</th>
      <td>0.473070</td>
      <td>0.408938</td>
      <td>-0.504452</td>
      <td>-0.214760</td>
    </tr>
  </tbody>
</table>
<p>100 rows × 4 columns</p>
</div>



戻すときはsetをresetに変えればOKです。(第2引数にsetした値の10も指定しなきゃいけないんだ・・・)


```python
pd.reset_option("display.max_rows", 10)
df
```




<div>
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.044887</td>
      <td>1.109229</td>
      <td>-1.404712</td>
      <td>0.014551</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-2.032691</td>
      <td>-0.435130</td>
      <td>-0.428953</td>
      <td>-0.537191</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.777178</td>
      <td>-0.435460</td>
      <td>0.848413</td>
      <td>-1.635667</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.213586</td>
      <td>-1.509976</td>
      <td>-0.635302</td>
      <td>-0.138209</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0.639734</td>
      <td>0.446097</td>
      <td>-1.312515</td>
      <td>0.783796</td>
    </tr>
    <tr>
      <th>5</th>
      <td>-0.156756</td>
      <td>0.521311</td>
      <td>0.060626</td>
      <td>0.206347</td>
    </tr>
    <tr>
      <th>6</th>
      <td>0.591887</td>
      <td>1.441567</td>
      <td>0.587750</td>
      <td>-0.240194</td>
    </tr>
    <tr>
      <th>7</th>
      <td>-0.098514</td>
      <td>1.053005</td>
      <td>0.072088</td>
      <td>-0.891726</td>
    </tr>
    <tr>
      <th>8</th>
      <td>1.484554</td>
      <td>-0.360987</td>
      <td>-1.724210</td>
      <td>-1.516901</td>
    </tr>
    <tr>
      <th>9</th>
      <td>-0.918722</td>
      <td>0.344975</td>
      <td>-0.439208</td>
      <td>-1.284894</td>
    </tr>
    <tr>
      <th>10</th>
      <td>-0.223029</td>
      <td>-0.107058</td>
      <td>1.234283</td>
      <td>-1.055316</td>
    </tr>
    <tr>
      <th>11</th>
      <td>-0.806544</td>
      <td>0.744367</td>
      <td>0.594333</td>
      <td>-0.993136</td>
    </tr>
    <tr>
      <th>12</th>
      <td>-0.680134</td>
      <td>1.570801</td>
      <td>1.204924</td>
      <td>-0.859910</td>
    </tr>
    <tr>
      <th>13</th>
      <td>-0.639150</td>
      <td>-0.004267</td>
      <td>-0.691408</td>
      <td>-0.214076</td>
    </tr>
    <tr>
      <th>14</th>
      <td>-0.219878</td>
      <td>-0.514751</td>
      <td>-1.332166</td>
      <td>0.570380</td>
    </tr>
    <tr>
      <th>15</th>
      <td>1.990532</td>
      <td>-1.174292</td>
      <td>-0.118421</td>
      <td>-0.113356</td>
    </tr>
    <tr>
      <th>16</th>
      <td>0.653598</td>
      <td>0.100153</td>
      <td>1.636529</td>
      <td>0.052311</td>
    </tr>
    <tr>
      <th>17</th>
      <td>-1.045861</td>
      <td>-0.064809</td>
      <td>-0.433254</td>
      <td>0.964098</td>
    </tr>
    <tr>
      <th>18</th>
      <td>0.231979</td>
      <td>0.067611</td>
      <td>-1.253458</td>
      <td>-1.037114</td>
    </tr>
    <tr>
      <th>19</th>
      <td>1.344306</td>
      <td>-0.936234</td>
      <td>0.594781</td>
      <td>0.105511</td>
    </tr>
    <tr>
      <th>20</th>
      <td>-0.413086</td>
      <td>-0.486708</td>
      <td>-0.911816</td>
      <td>1.050004</td>
    </tr>
    <tr>
      <th>21</th>
      <td>0.973888</td>
      <td>-1.365909</td>
      <td>-3.741730</td>
      <td>-1.507470</td>
    </tr>
    <tr>
      <th>22</th>
      <td>-0.821778</td>
      <td>0.355387</td>
      <td>-1.467101</td>
      <td>0.362862</td>
    </tr>
    <tr>
      <th>23</th>
      <td>0.005682</td>
      <td>-0.254259</td>
      <td>1.408601</td>
      <td>0.772690</td>
    </tr>
    <tr>
      <th>24</th>
      <td>0.761357</td>
      <td>-0.431552</td>
      <td>1.230341</td>
      <td>1.244104</td>
    </tr>
    <tr>
      <th>25</th>
      <td>0.433293</td>
      <td>-0.185350</td>
      <td>0.937934</td>
      <td>-0.913643</td>
    </tr>
    <tr>
      <th>26</th>
      <td>-0.202213</td>
      <td>0.528685</td>
      <td>-0.745797</td>
      <td>-2.023442</td>
    </tr>
    <tr>
      <th>27</th>
      <td>1.285016</td>
      <td>0.756849</td>
      <td>0.636789</td>
      <td>0.035517</td>
    </tr>
    <tr>
      <th>28</th>
      <td>-2.249611</td>
      <td>1.001626</td>
      <td>-0.071847</td>
      <td>-0.490456</td>
    </tr>
    <tr>
      <th>29</th>
      <td>-0.827190</td>
      <td>-0.157449</td>
      <td>-1.775256</td>
      <td>0.680590</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>70</th>
      <td>-0.283343</td>
      <td>1.396622</td>
      <td>-1.375399</td>
      <td>-0.297667</td>
    </tr>
    <tr>
      <th>71</th>
      <td>-1.792001</td>
      <td>1.488617</td>
      <td>-0.047619</td>
      <td>0.584341</td>
    </tr>
    <tr>
      <th>72</th>
      <td>-1.472638</td>
      <td>-3.259683</td>
      <td>-0.706456</td>
      <td>0.508512</td>
    </tr>
    <tr>
      <th>73</th>
      <td>0.743772</td>
      <td>-0.313420</td>
      <td>1.423694</td>
      <td>-1.095836</td>
    </tr>
    <tr>
      <th>74</th>
      <td>-0.923600</td>
      <td>-0.320489</td>
      <td>-0.920354</td>
      <td>-0.676194</td>
    </tr>
    <tr>
      <th>75</th>
      <td>-0.382733</td>
      <td>0.748782</td>
      <td>0.510318</td>
      <td>0.190481</td>
    </tr>
    <tr>
      <th>76</th>
      <td>-0.834656</td>
      <td>-0.927456</td>
      <td>1.718550</td>
      <td>0.244518</td>
    </tr>
    <tr>
      <th>77</th>
      <td>-0.537161</td>
      <td>0.315323</td>
      <td>0.243676</td>
      <td>-1.853278</td>
    </tr>
    <tr>
      <th>78</th>
      <td>-0.549080</td>
      <td>0.659434</td>
      <td>-0.627163</td>
      <td>1.142092</td>
    </tr>
    <tr>
      <th>79</th>
      <td>1.497259</td>
      <td>-0.183383</td>
      <td>-0.931365</td>
      <td>-0.712263</td>
    </tr>
    <tr>
      <th>80</th>
      <td>0.809390</td>
      <td>0.696450</td>
      <td>-0.949674</td>
      <td>0.333511</td>
    </tr>
    <tr>
      <th>81</th>
      <td>0.107922</td>
      <td>0.323430</td>
      <td>1.218619</td>
      <td>-0.486692</td>
    </tr>
    <tr>
      <th>82</th>
      <td>-0.969837</td>
      <td>0.585856</td>
      <td>1.138128</td>
      <td>0.399262</td>
    </tr>
    <tr>
      <th>83</th>
      <td>-0.423241</td>
      <td>0.855566</td>
      <td>-1.322747</td>
      <td>-0.313059</td>
    </tr>
    <tr>
      <th>84</th>
      <td>-0.708709</td>
      <td>-1.031457</td>
      <td>-0.361363</td>
      <td>1.389282</td>
    </tr>
    <tr>
      <th>85</th>
      <td>-1.155997</td>
      <td>-0.054445</td>
      <td>-1.037225</td>
      <td>-2.020944</td>
    </tr>
    <tr>
      <th>86</th>
      <td>-0.509943</td>
      <td>1.279200</td>
      <td>-1.473619</td>
      <td>1.070197</td>
    </tr>
    <tr>
      <th>87</th>
      <td>0.593176</td>
      <td>0.660035</td>
      <td>0.809127</td>
      <td>-1.455174</td>
    </tr>
    <tr>
      <th>88</th>
      <td>1.867072</td>
      <td>-0.697688</td>
      <td>-1.144857</td>
      <td>-1.740410</td>
    </tr>
    <tr>
      <th>89</th>
      <td>0.500170</td>
      <td>-0.266405</td>
      <td>0.226681</td>
      <td>-0.800579</td>
    </tr>
    <tr>
      <th>90</th>
      <td>-1.746962</td>
      <td>1.414762</td>
      <td>0.789651</td>
      <td>-1.362200</td>
    </tr>
    <tr>
      <th>91</th>
      <td>-0.289363</td>
      <td>1.300986</td>
      <td>0.210491</td>
      <td>1.529958</td>
    </tr>
    <tr>
      <th>92</th>
      <td>-0.852068</td>
      <td>-0.048329</td>
      <td>-0.269035</td>
      <td>0.250980</td>
    </tr>
    <tr>
      <th>93</th>
      <td>-0.388686</td>
      <td>-1.312654</td>
      <td>-1.036473</td>
      <td>-1.297159</td>
    </tr>
    <tr>
      <th>94</th>
      <td>-1.035741</td>
      <td>0.097650</td>
      <td>0.454851</td>
      <td>2.067922</td>
    </tr>
    <tr>
      <th>95</th>
      <td>-0.288322</td>
      <td>0.132045</td>
      <td>-0.405144</td>
      <td>-0.542612</td>
    </tr>
    <tr>
      <th>96</th>
      <td>-0.631888</td>
      <td>-0.487683</td>
      <td>0.684156</td>
      <td>-0.114015</td>
    </tr>
    <tr>
      <th>97</th>
      <td>-0.405025</td>
      <td>1.719596</td>
      <td>0.451788</td>
      <td>0.930674</td>
    </tr>
    <tr>
      <th>98</th>
      <td>0.146326</td>
      <td>1.032291</td>
      <td>-0.474848</td>
      <td>0.847260</td>
    </tr>
    <tr>
      <th>99</th>
      <td>0.473070</td>
      <td>0.408938</td>
      <td>-0.504452</td>
      <td>-0.214760</td>
    </tr>
  </tbody>
</table>
<p>100 rows × 4 columns</p>
</div>



# head/tailメソッド

df.head()とかdf.tail()すれば表示を少なくできます。


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




<div>
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.044887</td>
      <td>1.109229</td>
      <td>-1.404712</td>
      <td>0.014551</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-2.032691</td>
      <td>-0.435130</td>
      <td>-0.428953</td>
      <td>-0.537191</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.777178</td>
      <td>-0.435460</td>
      <td>0.848413</td>
      <td>-1.635667</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.213586</td>
      <td>-1.509976</td>
      <td>-0.635302</td>
      <td>-0.138209</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0.639734</td>
      <td>0.446097</td>
      <td>-1.312515</td>
      <td>0.783796</td>
    </tr>
  </tbody>
</table>
</div>




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




<div>
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>95</th>
      <td>-0.288322</td>
      <td>0.132045</td>
      <td>-0.405144</td>
      <td>-0.542612</td>
    </tr>
    <tr>
      <th>96</th>
      <td>-0.631888</td>
      <td>-0.487683</td>
      <td>0.684156</td>
      <td>-0.114015</td>
    </tr>
    <tr>
      <th>97</th>
      <td>-0.405025</td>
      <td>1.719596</td>
      <td>0.451788</td>
      <td>0.930674</td>
    </tr>
    <tr>
      <th>98</th>
      <td>0.146326</td>
      <td>1.032291</td>
      <td>-0.474848</td>
      <td>0.847260</td>
    </tr>
    <tr>
      <th>99</th>
      <td>0.473070</td>
      <td>0.408938</td>
      <td>-0.504452</td>
      <td>-0.214760</td>
    </tr>
  </tbody>
</table>
</div>



# head/tailをつなげる
しかしながら、これだけでは不十分で、データの頭とおしりを同時に確認したい局面がよくあるんですよね。headとtail両方同時に出力できたら便利なのに・・・。

やっている人がいました

[Stack Overflow - python pandas select both head and tail](https://stackoverflow.com/questions/42504984/python-pandas-select-both-head-and-tail/47958056#47958056)

```python
df.head().append(df.tail())
```

とすればheadとtailをつなげてくれます。


```python
df.head().append(df.tail())
```




<div>
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.044887</td>
      <td>1.109229</td>
      <td>-1.404712</td>
      <td>0.014551</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-2.032691</td>
      <td>-0.435130</td>
      <td>-0.428953</td>
      <td>-0.537191</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.777178</td>
      <td>-0.435460</td>
      <td>0.848413</td>
      <td>-1.635667</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.213586</td>
      <td>-1.509976</td>
      <td>-0.635302</td>
      <td>-0.138209</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0.639734</td>
      <td>0.446097</td>
      <td>-1.312515</td>
      <td>0.783796</td>
    </tr>
    <tr>
      <th>95</th>
      <td>-0.288322</td>
      <td>0.132045</td>
      <td>-0.405144</td>
      <td>-0.542612</td>
    </tr>
    <tr>
      <th>96</th>
      <td>-0.631888</td>
      <td>-0.487683</td>
      <td>0.684156</td>
      <td>-0.114015</td>
    </tr>
    <tr>
      <th>97</th>
      <td>-0.405025</td>
      <td>1.719596</td>
      <td>0.451788</td>
      <td>0.930674</td>
    </tr>
    <tr>
      <th>98</th>
      <td>0.146326</td>
      <td>1.032291</td>
      <td>-0.474848</td>
      <td>0.847260</td>
    </tr>
    <tr>
      <th>99</th>
      <td>0.473070</td>
      <td>0.408938</td>
      <td>-0.504452</td>
      <td>-0.214760</td>
    </tr>
  </tbody>
</table>
</div>



# head/tailをつなげたやつのタイプを短くする

lambda式を使ってメソッドとして登録しちゃいます。
メソッドの名前はlessとしましたが、好きな名前をつけて、`~/.ipython/profile_default/startup`以下にスクリプトを保存していつでも呼び出せるようにしちゃいましょう。

```python:~/.ipython/profile_default/startup/less.ipy
pd.DataFrame.less = lambda df, n=10: self.head(n//2).append(self.tail(n//2))
```


```python
df.less()
```




<div>
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.044887</td>
      <td>1.109229</td>
      <td>-1.404712</td>
      <td>0.014551</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-2.032691</td>
      <td>-0.435130</td>
      <td>-0.428953</td>
      <td>-0.537191</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.777178</td>
      <td>-0.435460</td>
      <td>0.848413</td>
      <td>-1.635667</td>
    </tr>
    <tr>
      <th>3</th>
      <td>-0.213586</td>
      <td>-1.509976</td>
      <td>-0.635302</td>
      <td>-0.138209</td>
    </tr>
    <tr>
      <th>4</th>
      <td>0.639734</td>
      <td>0.446097</td>
      <td>-1.312515</td>
      <td>0.783796</td>
    </tr>
    <tr>
      <th>95</th>
      <td>-0.288322</td>
      <td>0.132045</td>
      <td>-0.405144</td>
      <td>-0.542612</td>
    </tr>
    <tr>
      <th>96</th>
      <td>-0.631888</td>
      <td>-0.487683</td>
      <td>0.684156</td>
      <td>-0.114015</td>
    </tr>
    <tr>
      <th>97</th>
      <td>-0.405025</td>
      <td>1.719596</td>
      <td>0.451788</td>
      <td>0.930674</td>
    </tr>
    <tr>
      <th>98</th>
      <td>0.146326</td>
      <td>1.032291</td>
      <td>-0.474848</td>
      <td>0.847260</td>
    </tr>
    <tr>
      <th>99</th>
      <td>0.473070</td>
      <td>0.408938</td>
      <td>-0.504452</td>
      <td>-0.214760</td>
    </tr>
  </tbody>
</table>
</div>



引数指定で出力行を変えられます。


```python
df.less(2)
```




<div>
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.044887</td>
      <td>1.109229</td>
      <td>-1.404712</td>
      <td>0.014551</td>
    </tr>
    <tr>
      <th>99</th>
      <td>0.473070</td>
      <td>0.408938</td>
      <td>-0.504452</td>
      <td>-0.214760</td>
    </tr>
  </tbody>
</table>
</div>



jupyter notebookで人に見せる記事を書くときはググれる情報である`set_option`で出力を常に変えたり、`ix, iloc, loc`メソッドで出力を絞ると良いですが、下書き段階やipythonで一時的に出力行を絞るときは`tail`と`head`の合わせ技の方が有効かもしれません。