[Tut] How to Convert Pandas DataFrame/Series to NumPy Array? - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Convert Pandas DataFrame/Series to NumPy Array? (/thread-100136.html) |
[Tut] How to Convert Pandas DataFrame/Series to NumPy Array? - xSicKxBot - 10-24-2022 How to Convert Pandas DataFrame/Series to NumPy Array? <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"821905","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </div> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Programming Challenge</strong>: Given a Pandas DataFrame or a Pandas Series object. How to convert them to a NumPy array?</p> <div class="wp-block-image"> <figure class="aligncenter size-large"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/10/pandas_to_numpy-1024x576.jpg" alt="How to Convert Pandas DataFrame/Series to NumPy Array?" class="wp-image-822123" srcset="https://blog.finxter.com/wp-content/uploads/2022/10/pandas_to_numpy-1024x576.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2022/10/pandas_to_numpy-300x169.jpg 300w, https://blog.finxter.com/wp-content/uploads/2022/10/pandas_to_numpy-768x432.jpg 768w, https://blog.finxter.com/wp-content/uploads/2022/10/pandas_to_numpy.jpg 1280w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure> </div> <p>In this short tutorial, you’ll learn (1) how to <strong>convert a 1D pandas Series</strong> to a <a href="https://blog.finxter.com/numpy-tutorial/" data-type="post" data-id="1356" target="_blank" rel="noreferrer noopener">NumPy</a> array, and (2) how to <strong>convert a 2D pandas DataFrame</strong> to an array. Let’s get started with the first! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> <h2>Convert Pandas Series to NumPy Array</h2> <p>First, let’s create a <a href="https://blog.finxter.com/pandas-quickstart/" data-type="post" data-id="16511" target="_blank" rel="noreferrer noopener">Pandas</a> Series.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd # create dataframe df df = pd.Series([22,21,20,14], name= 'GSTitles', index= ['Nadal','Djokovic','Federer','Sampras']) print(df) </pre> <p>Here’s the resulting Series <code>df</code>:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Nadal 22 Djokovic 21 Federer 20 Sampras 14 Name: GSTitles, dtype: int64</pre> <p>Now that we have our Pandas Series, you can convert this to a NumPy Array using the <code>DataFrame.to_numpy()</code> method.</p> <p>Like so:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(df.to_numpy()) # [22 21 20 14]</pre> <p>The resulting object is a <a href="https://blog.finxter.com/numpy-tutorial/" data-type="post" data-id="1356" target="_blank" rel="noreferrer noopener">NumPy</a> array:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(type(df.to_numpy())) # <class 'numpy.ndarray'></pre> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/26a1.png" alt="⚡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Attention</strong>: There is also the <code>.values()</code> method, but that is being deprecated now – when you look at the Pandas documentation, there is a warning <em>“We recommend using <code>DataFrame.to_numpy</code> instead”</em>. </p> <p>With this method, only the values in the DataFrame or Series will return. The index labels will be removed.</p> <p>Here’s how that’ll work:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(df.values) # [22 21 20 14]</pre> <p>This was a 1-dimensional array or a Series. Let’s move on to the 2D case next. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> <h2>Convert DataFrame to NumPy Array</h2> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: Let’s try with a two-dimensional DataFrame — how to convert it to a NumPy array?</p> <p>First, let’s <a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print</a> the dimension of the previous Series to confirm that it was, indeed, a 1D data structure:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(df.ndim) # 1</pre> <p>Next, you create a 2D DataFrame object:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd # Create a 2D DataFrame object df2 = pd.DataFrame(data={'Nadal': [2, 14, 2, 4], 'Djokovic': [9, 2, 7, 3], 'Federer': [6, 1, 8, 5], 'Sampras': [2, 0, 7, 5]}, index=['AO', 'F', 'W', 'US']) print(df2) </pre> <p>Here’s the resulting DataFrame:</p> <figure class="wp-block-table is-style-stripes"> <table> <thead> <tr> <th></th> <th>Nadal</th> <th>Djokovic</th> <th>Federer</th> <th>Sampras</th> </tr> </thead> <tbody> <tr> <th>AO</th> <td>2</td> <td>9</td> <td>6</td> <td>2</td> </tr> <tr> <th>F</th> <td>14</td> <td>2</td> <td>1</td> <td>0</td> </tr> <tr> <th>W</th> <td>2</td> <td>7</td> <td>8</td> <td>7</td> </tr> <tr> <th>US</th> <td>4</td> <td>3</td> <td>5</td> <td>5</td> </tr> </tbody> </table> </figure> <p>Now, let’s dive into the conversion of this DataFrame to a NumPy array by using the <code>DataFrame.to_numpy()</code> method.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Convert this DataFrame to a NumPy array print(df2.to_numpy())</pre> <p>The output shows a NumPy array from the 2D DataFrame — great! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f47e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[[ 2 9 6 2] [14 2 1 0] [ 2 7 8 7] [ 4 3 5 5]]</pre> <p>You can see that all indexing metadata has been stripped away from the resulting NumPy array!</p> <h2>Convert Specific Columns from DataFrame to NumPy Array</h2> <p class="has-global-color-8-background-color has-background">You can also convert specific columns of a Pandas DataFrame by accessing the columns using pandas indexing and calling the <code>.to_numpy()</code> method on the resulting view object.</p> <p>Here’s an example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(df2[['Djokovic', 'Federer']].to_numpy())</pre> <p>The output:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[[9 6] [2 1] [7 8] [3 5]]</pre> <h2>Summary</h2> <p>You can convert a Pandas DataFrame or a Pandas Series object to a NumPy array by means of the <code>df.to_numpy()</code> method. The indexing metadata will be removed.</p> <p>You can also convert specific columns of a Pandas DataFrame by accessing the columns using <a href="https://blog.finxter.com/pandas-dataframe-indexing/" data-type="post" data-id="64801" target="_blank" rel="noreferrer noopener">pandas indexing</a> and calling the <code>.to_numpy()</code> method on the resulting view object.</p> <hr class="wp-block-separator has-alpha-channel-opacity"/> <p>Thanks for reading through the whole tutorial! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> </div> https://www.sickgaming.net/blog/2022/10/23/how-to-convert-pandas-dataframe-series-to-numpy-array/ |