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> |
1 |
<span role="presentation"><span class="cm-comment"># dfの"apple"列が5以上かつ"kiwifruit"列が5以上の値をもつ行を含むDataFrameをdfに代入してください</span></span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-variable">df</span> = <span class="cm-variable">df</span>.<span class="cm-property">loc</span>[<span class="cm-variable">df</span>[<span class="cm-string">"apple"</span>] <span class="cm-operator">></span>= <span class="cm-number">5</span>]</span> |
1 |
<span role="presentation"><span class="cm-variable">df</span> = <span class="cm-variable">df</span>.<span class="cm-property">loc</span>[<span class="cm-variable">df</span><span class=" CodeMirror-matchingbracket">[</span><span class="cm-string">"kiwifruit"</span><span class=" CodeMirror-matchingbracket">]</span> <span class="cm-operator">></span>= <span class="cm-number">5</span> ]</span> |
1 |
<span role="presentation"><span class="cm-builtin">print</span>(<span class="cm-variable">df</span>)</span> |
コメントを残す