列ごとの平均値、最大値、最小値等統計的情報を要約統計量と呼ぶ。
DataFrameの変数に対して、変数.describe()
は変数の列ごとの
個数、平均値、標準偏差、最小値、四分位数、最大値を返す。
DataFrameのインデックスの数字は統計量の名前に置き換わる
1 |
<span role="presentation"><span class="cm-keyword">import</span> <span class="cm-variable">numpy</span> <span class="cm-keyword">as</span> <span class="cm-variable">np</span></span> |
1 |
<span role="presentation"><span class="cm-keyword">import</span> <span class="cm-variable">pandas</span> <span class="cm-keyword">as</span> <span class="cm-variable">pd</span></span> |
1 |
<span role="presentation"><span class="cm-variable">np</span>.<span class="cm-property">random</span>.<span class="cm-property">seed</span>(<span class="cm-number">0</span>)</span> |
1 |
<span role="presentation"><span class="cm-variable">columns</span> = [<span class="cm-string">"apple"</span>, <span class="cm-string">"orange"</span>, <span class="cm-string">"banana"</span>, <span class="cm-string">"strawberry"</span>, <span class="cm-string">"kiwifruit"</span>]</span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-comment"># DataFrameを生成し、列を追加</span></span> |
1 |
<span role="presentation"><span class="cm-variable">df</span> = <span class="cm-variable">pd</span>.<span class="cm-property">DataFrame</span>()</span> |
1 |
<span role="presentation"><span class="cm-keyword">for</span> <span class="cm-variable">column</span> <span class="cm-keyword">in</span> <span class="cm-variable">columns</span>:</span> |
1 |
<span role="presentation"> <span class="cm-variable">df</span>[<span class="cm-variable">column</span>] = <span class="cm-variable">np</span>.<span class="cm-property">random</span>.<span class="cm-property">choice</span>(<span class="cm-builtin">range</span>(<span class="cm-number">1</span>, <span class="cm-number">11</span>), <span class="cm-number">10</span>)</span> |
1 |
<span role="presentation"><span class="cm-variable">df</span>.<span class="cm-property">index</span> = <span class="cm-builtin">range</span>(<span class="cm-number">1</span>, <span class="cm-number">11</span>)</span> |
1 |
<span role="presentation"><span class="cm-builtin">print</span><span class=" CodeMirror-matchingbracket">(</span><span class="cm-variable">df</span><span class=" CodeMirror-matchingbracket">)</span></span> |
1 |
<span role="presentation"><span class="cm-comment"># dfの要約統計量のうち、"mean", "max", "min"を取り出してdf_desに代入してください</span></span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-variable">df_des</span> = <span class="cm-variable">df</span>.<span class="cm-property">describe</span>().<span class="cm-property">loc</span>[[<span class="cm-string">"mean"</span>, <span class="cm-string">"max"</span>, <span class="cm-string">"min"</span>]]</span> |
1 |
<span role="presentation"><span class="cm-builtin">print</span>(<span class="cm-variable">df_des</span>)</span> |
コメントを残す