{"id":129156,"date":"2022-10-23T17:10:38","date_gmt":"2022-10-23T17:10:38","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=821905"},"modified":"2022-10-23T17:10:38","modified_gmt":"2022-10-23T17:10:38","slug":"how-to-convert-pandas-dataframe-series-to-numpy-array","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/23\/how-to-convert-pandas-dataframe-series-to-numpy-array\/","title":{"rendered":"How to Convert Pandas DataFrame\/Series to NumPy Array?"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;821905&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" 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>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>In this short tutorial, you&#8217;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&#8217;s get started with the first! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Convert Pandas Series to NumPy Array<\/h2>\n<p>First, let&#8217;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>\n<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'])\nprint(df)\n<\/pre>\n<p>Here&#8217;s the resulting Series <code>df<\/code>:<\/p>\n<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\nDjokovic 21\nFederer 20\nSampras 14\nName: GSTitles, dtype: int64<\/pre>\n<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>\n<p>Like so:<\/p>\n<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())\n# [22 21 20 14]<\/pre>\n<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>\n<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()))\n# &lt;class 'numpy.ndarray'><\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/26a1.png\" alt=\"\u26a1\" 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 &#8211; when you look at the Pandas documentation, there is a warning <em>&#8220;We recommend using <code>DataFrame.to_numpy<\/code> instead&#8221;<\/em>. <\/p>\n<p>With this method, only the values in the DataFrame or Series will return. The index labels will be removed.<\/p>\n<p>Here&#8217;s how that&#8217;ll work:<\/p>\n<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)\n# [22 21 20 14]<\/pre>\n<p>This was a 1-dimensional array or a Series. Let&#8217;s move on to the 2D case next. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Convert DataFrame to NumPy Array<\/h2>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: Let&#8217;s try with a two-dimensional DataFrame &#8212; how to convert it to a NumPy array?<\/p>\n<p>First, let&#8217;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>\n<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)\n# 1<\/pre>\n<p>Next, you create a 2D DataFrame object:<\/p>\n<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\ndf2 = 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)\n<\/pre>\n<p>Here&#8217;s the resulting DataFrame:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>Nadal<\/th>\n<th>Djokovic<\/th>\n<th>Federer<\/th>\n<th>Sampras<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<th>AO<\/th>\n<td>2<\/td>\n<td>9<\/td>\n<td>6<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<th>F<\/th>\n<td>14<\/td>\n<td>2<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<th>W<\/th>\n<td>2<\/td>\n<td>7<\/td>\n<td>8<\/td>\n<td>7<\/td>\n<\/tr>\n<tr>\n<th>US<\/th>\n<td>4<\/td>\n<td>3<\/td>\n<td>5<\/td>\n<td>5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Now, let&#8217;s dive into the conversion of this DataFrame to a NumPy array by using the <code>DataFrame.to_numpy()<\/code> method.<\/p>\n<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\nprint(df2.to_numpy())<\/pre>\n<p>The output shows a NumPy array from the 2D DataFrame &#8212; great! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f47e.png\" alt=\"\ud83d\udc7e\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<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>\n<p>You can see that all indexing metadata has been stripped away from the resulting NumPy array!<\/p>\n<h2>Convert Specific Columns from DataFrame to NumPy Array<\/h2>\n<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>\n<p>Here&#8217;s an example:<\/p>\n<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>\n<p>The output:<\/p>\n<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>\n<h2>Summary<\/h2>\n<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>\n<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>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<p>Thanks for reading through the whole tutorial! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Programming Challenge: Given a Pandas DataFrame or a Pandas Series object. How to convert them to a NumPy array? In this short tutorial, you&#8217;ll learn (1) how to convert a 1D pandas Series to a NumPy array, and (2) how to convert a 2D pandas DataFrame to an array. Let&#8217;s get [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-129156","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=129156"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129156\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=129156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=129156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=129156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}