{"id":20465,"date":"2021-07-19T17:01:59","date_gmt":"2021-07-19T17:01:59","guid":{"rendered":"https:\/\/www.askpython.com\/?p=20465"},"modified":"2024-02-07T12:04:44","modified_gmt":"2024-02-07T12:04:44","slug":"pandas-dataframe-to-numpy-array","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/pandas-dataframe-to-numpy-array","title":{"rendered":"Converting Pandas DataFrame to Numpy Array [Step-By-Step]"},"content":{"rendered":"\n<p>Hello Reader! In this article, we will see what the data frame is and how to convert Pandas Dataframe to Numpy Array and vice versa. So Let&#8217;s begin:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>A data frame in Python is a two-dimensional, tabular data structure consisting of rows and columns defining different features of the data frame.<\/p>\n\n\n\n<p>We can create a data frame using the <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" data-type=\"post\" data-id=\"2986\">Pandas library<\/a> or we can import an already built data frame (.csv file) and work on it. You can install Pandas using the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-pip\" data-type=\"post\" data-id=\"3848\">pip command<\/a>. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install pandas\n<\/pre><\/div>\n\n\n<p>The above-written code installs pandas and we are all ready to use different functions of the Pandas library. In the same way, we will install the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" data-type=\"post\" data-id=\"1070\">numpy library<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install numpy\n<\/pre><\/div>\n\n\n<p>So first, we will see the conversion of this tabular structure (pandas data frame) into a numpy array. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Converting Pandas Dataframe to Numpy Array <\/h3>\n\n\n\n<p>We can do this by using<em> dataframe.to_numpy() <\/em>method. This will convert the given Pandas Dataframe to Numpy Array.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Let us create two data frames which we will be using for this tutorial.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#importing pandas\nimport pandas as pd\n\n#creating dataframes\nstudent_data = {&quot;Name&quot;: &#x5B;&#039;Alice&#039;, &#039;Sam&#039;, &#039;Kevin&#039;, &#039;Max&#039;, &#039;Tom&#039;],\n        &quot;exam_no&quot;: &#x5B;201, 202, 203, 204, 205],\n        &quot;Result&quot;: &#x5B;&#039;Pass&#039;, &#039;Pass&#039;, &#039;Fail&#039;, &#039;Pass&#039;, &#039;Fail&#039;]}\n\nset_of_numbers = {&quot;Numbers&quot;: &#x5B;&#039;134&#039;, &#039;273&#039;, &#039;325&#039;,&#039;69.21&#039;,&#039;965&#039;]}\n\nprint(&quot;This is our first dataset :&quot;)\nstudent_dataframe = pd.DataFrame(student_data)\nprint(&quot;\\n&quot;,student_dataframe)\n\nprint(&quot;\\nThis is our second dataset :&quot;)\nnumbers_dataframe = pd.DataFrame(set_of_numbers)\nprint(&quot;\\n&quot;,numbers_dataframe)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>We have created two data frames: <strong>student_data <\/strong>and <strong>set_of_numbers<\/strong>. Our data frames look like this:<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/dataframes-1024x300.png\" alt=\"Dataframes\" class=\"wp-image-20468\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/dataframes-1024x300.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/dataframes-300x88.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/dataframes-768x225.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/dataframes.png 1045w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>Now, before converting the Pandas Dataframe to Numpy Array, let&#8217;s see the type :<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(type(student_dataframe))\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(type(numbers_dataframe))\n<\/pre><\/div>\n\n\n<p>The output for both the statements above is the same. I.e.,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;class &#039;pandas.core.frame.DataFrame&#039;&gt;\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>To convert this Pandas Dataframe to Numpy Array, run the code given below <\/li><\/ul>\n\n\n\n<p>Converting student_data to <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstudent_array = student_dataframe.to_numpy()\nprint(student_array)\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;&#039;Alice&#039; 201 &#039;Pass&#039;]\n &#x5B;&#039;Sam&#039; 202 &#039;Pass&#039;]\n &#x5B;&#039;Kevin&#039; 203 &#039;Fail&#039;]\n &#x5B;&#039;Max&#039; 204 &#039;Pass&#039;]\n &#x5B;&#039;Tom&#039; 205 &#039;Fail&#039;]]\n<\/pre><\/div>\n\n\n<p>For the second data frame (set_of_numbers)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumbers_array = numbers_dataframe.to_numpy()\nprint(numbers_array)\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;&#039;134&#039;]\n &#x5B;&#039;273&#039;]\n &#x5B;&#039;325&#039;]\n &#x5B;&#039;69.21&#039;]\n &#x5B;&#039;965&#039;]]\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>We can also check the datatypes for both the arrays :<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(type(student_array))\nprint(type(numbers_array))\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;class &#039;numpy.ndarray&#039;&gt;\n&lt;class &#039;numpy.ndarray&#039;&gt;\n<\/pre><\/div>\n\n\n<p>So, we can clearly see that we converted our Pandas Dataframe to Numpy Array in just a few steps. This is the simplest way to handle data frames and their conversion.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Further, we can also change the data type of columns in a data frame. Considering our second data frame, it consists of some integer values and some floating values, let&#8217;s try to change all of them to float.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(numbers_dataframe.to_numpy(dtype =&#039;float64&#039;))\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;134.  ]\n &#x5B;273.  ]\n &#x5B;325.  ]\n &#x5B; 69.21]\n &#x5B;965.  ]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Converting Numpy Arrays to Pandas Dataframes<\/h3>\n\n\n\n<p>Now that you have understood the conversion of the Pandas Dataframe to Numpy Array, we may need to convert the data back to Numpy Array. Let&#8217;s see how to do that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, define a <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" data-type=\"post\" data-id=\"1070\">numpy array<\/a>. And then perform the conversion using pandas.DataFrame() function of pandas library.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#importing pandas and numpy\nimport pandas as pd\nimport numpy as np\n\n#defining numpy array \narr1 = np.array(&#x5B;&#x5B;1,6,4,5], &#x5B;3,7,2,4], &#x5B;9,5,3,7]])\nprint(&quot;Numpy array : &quot;)\nprint(arr1)\n<\/pre><\/div>\n\n\n<p>So, our array is like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nNumpy array : \n&#x5B;&#x5B;1 6 4 5]\n &#x5B;3 7 2 4]\n &#x5B;9 5 3 7]]\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>Now, converting this to pandas dataframe:<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#converting array to dataframe\ndf = pd.DataFrame(arr1)\nprint(&quot;\\npandas dataframe :&quot;)\ndf\n<\/pre><\/div>\n\n\n<p>The converted data frame is :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/pandas_df.png\" alt=\"Pandas Dataframe to Numpy Array\" class=\"wp-image-20511\" width=\"707\" height=\"168\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/pandas_df.png 709w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/pandas_df-300x71.png 300w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>Checking the type of dataframe:<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntype(df)\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npandas.core.frame.DataFrame\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>We can also give our own headers to rows and columns of the data frames. Headers for rows can be given using <em>index<\/em> keyword and, for columns, we use the <em>columns<\/em> keyword.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#converting and providing headers\ndf = pd.DataFrame(arr1, index = &#x5B;&quot;1&quot;,&quot;2&quot;,&quot;3&quot;], columns = &#x5B;&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot; ])\nprint(&quot;\\npandas dataframe :&quot;)\ndf\n<\/pre><\/div>\n\n\n<p>This will make our data frame look like this :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"780\" height=\"203\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/pd_df.png\" alt=\"Pandas Dataframe to Numpy Array\" class=\"wp-image-20512\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/pd_df.png 780w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/pd_df-300x78.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/pd_df-768x200.png 768w\" sizes=\"auto, (max-width: 780px) 100vw, 780px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>With this, we come to the end of this article. In this article you understood<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The basics of pandas dataframe and numpy array<\/li><li>How to convert pandas data frame to numpy array<\/li><li>How to convert numpy array to pandas dataframe<\/li><\/ul>\n\n\n\n<p>I hope this article was useful to you. Thank you! \ud83d\ude42<\/p>\n\n\n\n<p><strong><em>References &#8211; <\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em><a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/version\/0.24\/reference\/api\/pandas.DataFrame.to_numpy.html\" target=\"_blank\" rel=\"noopener\">https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.DataFrame.to_numpy.html<\/a><\/em><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/13187778\/convert-pandas-dataframe-to-numpy-array\/\" target=\"_blank\" rel=\"noopener\"><em>https:\/\/stackoverflow.com\/questions\/13187778\/convert-pandas-dataframe-to-numpy-array\/<\/em><\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hello Reader! In this article, we will see what the data frame is and how to convert Pandas Dataframe to Numpy Array and vice versa. So Let&#8217;s begin: Introduction A data frame in Python is a two-dimensional, tabular data structure consisting of rows and columns defining different features of the data frame. We can create [&hellip;]<\/p>\n","protected":false},"author":34,"featured_media":20532,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-20465","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/20465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=20465"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/20465\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/20532"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=20465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=20465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=20465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}