DataFrameの行の名前をインデックス(横) 指定した要素の名前、数だけ右に向かって出力
DataFrameの列の名前をカラム(縦)0~nまで要素の数だけ縦に自動で付記
DataFrameは、Seriesを複数束る2次元のデータ構造。
pandas.DataFrame()
にSeriesを渡し、DataFrameを生成行には0から昇順に番号がつきます。
1 2 |
<span class="cm-variable">pandas</span>.<span class="cm-property">DataFrame</span>([<span class="cm-variable">Series</span>, <span class="cm-variable">Series</span>, ...]) |
バリューにリストの辞書型を用いても作成可能
リスト型の長さは等しくする
コード
1 2 3 4 5 6 |
<span class="cm-variable">data</span> = {<span class="cm-string">"fruits"</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 class="cm-string">"year"</span>: [<span class="cm-number">2001</span>, <span class="cm-number">2002</span>, <span class="cm-number">2001</span>, <span class="cm-number">2008</span>, <span class="cm-number">2006</span>], <span class="cm-string">"time"</span>: [<span class="cm-number">1</span>, <span class="cm-number">4</span>, <span class="cm-number">5</span>, <span class="cm-number">6</span>, <span class="cm-number">3</span>]} <span class="cm-variable">df</span> = <span class="cm-variable">pd</span>.<span class="cm-property">DataFrame</span>(<span class="cm-variable">data</span>) <span class="cm-builtin">print</span>(<span class="cm-variable">df</span>) |
出力結果
1 2 3 4 5 6 7 8 9 10 11 |
fruits time year 0 apple 1 2001 1 orange 4 2002 2 banana 5 2001 3 strawberry 6 2008 4 kiwifruit 3 2006 例 |
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> |
1 |
<span role="presentation"><span class="cm-variable">index</span> = [<span class="cm-string">"a"</span>, <span class="cm-string">"b"</span>, <span class="cm-string">"c"</span>, <span class="cm-string">"d"</span>, <span class="cm-string">"e"</span>]</span> |
1 |
<span role="presentation"><span class="cm-variable">data1</span> = [<span class="cm-number">10</span>, <span class="cm-number">5</span>, <span class="cm-number">8</span>, <span class="cm-number">12</span>, <span class="cm-number">3</span>]</span> |
1 |
<span role="presentation"><span class="cm-variable">data2</span> = [<span class="cm-number">30</span>, <span class="cm-number">25</span>, <span class="cm-number">12</span>, <span class="cm-number">10</span>, <span class="cm-number">8</span>]</span> |
1 |
<span role="presentation"><span class="cm-variable">series1</span> = <span class="cm-variable">pd</span>.<span class="cm-property">Series</span>(<span class="cm-variable">data1</span>, <span class="cm-variable">index</span>=<span class="cm-variable">index</span>)</span> |
1 |
<span role="presentation"><span class="cm-variable">series2</span> = <span class="cm-variable">pd</span>.<span class="cm-property">Series</span>(<span class="cm-variable">data2</span>, <span class="cm-variable">index</span>=<span class="cm-variable">index</span>)</span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-comment"># series1, seires2からDataFrameを生成してdfに代入</span></span> |
1 |
1 |
<span role="presentation"><span class="cm-variable">df</span> = <span class="cm-variable">pd</span>.<span class="cm-property">DataFrame</span>([<span class="cm-variable">series1</span>,<span class="cm-variable">series2</span>])</span> |
1 |
<span role="presentation"><span class="cm-comment"># 出力</span></span> |
1 |
<span role="presentation"><span class="cm-builtin">print</span>(<span class="cm-variable">df</span>)</span> |
1 |
コメントを残す