{"id":1233,"date":"2024-01-02T13:00:00","date_gmt":"2024-01-02T13:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1233"},"modified":"2024-01-03T09:06:45","modified_gmt":"2024-01-03T09:06:45","slug":"pandas-dataframe-to-numpy-array","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/pandas-dataframe-to-numpy-array\/","title":{"rendered":"Convert Pandas DataFrame to NumPy Array (with code)"},"content":{"rendered":"\n<p>The Pandas library in Python provides multi-data type analysis with lots of built-in classes and functions. The Numpy library on the other hand provides multi-dimensional analysis of numeric data. Both libraries provide a robust tool for two-dimensional data analysis. In this article, we will discuss the various methods we can use to convert a Pandas DataFrame to a Numpy Array.<\/p>\n\n\n\n<p>But before that, let&#8217;s revise both of them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is the Pandas Dataframe?<\/strong><\/h3>\n\n\n\n<p>The Pandas DataFrame is a two-dimensional, size-mutable, and potentially miscellaneous type of data structure present in tabular form. It consists of labeled axes (rows and columns) and offers a wide range of functionalities for data manipulation, cleaning, and analysis. <\/p>\n\n\n\n<p>Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\ndata = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],\n        'Age': [25, 30, 35, 40],\n        'City': ['New York', 'Los Angeles', 'San Francisco', 'Chicago']}\n\ndf = pd.DataFrame(data)\n\n# Display the DataFrame\nprint('DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>DataFrame:\n       Name  Age           City\n0    Alice   25       New York\n1      Bob   30    Los Angeles\n2  Charlie   35  San Francisco\n3    David   40        Chicago\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What is the Numpy Array in Python?<\/strong><\/h3>\n\n\n\n<p>NumPy stands for Numerical Python and is a fundamental package for scientific computing in Python. It provides powerful N-dimensional array objects, which are essential for performing mathematical and logical operations on large datasets efficiently. <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import numpy as np\n\n# List of lists\nlist_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n\n# Convert to NumPy array\nnumpy_array = np.array(list_of_lists)\n\n# Display the NumPy array\nprint(&quot;NumPy Array: \\n&quot;, numpy_array)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>NumPy Array: \n &#91;&#91;1 2 3]\n &#91;4 5 6]\n &#91;7 8 9]]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Convert Pandas DataFrame to Numpy Array?<\/strong><\/h2>\n\n\n\n<p><strong>The major difference between DataFrames and NumPy would be that NumPy arrays have the same type of data structures which is why they are known as homogeneous sets of data structures.<\/strong> There are many more differences between both the libraries, which you can learn in this guide for <a href=\"https:\/\/favtutor.com\/blogs\/numpy-vs-pandas\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/blogs\/numpy-vs-pandas\">Pandas vs Numpy<\/a>.<\/p>\n\n\n\n<p>Here are various ways to convert a Pandas Dataframe to NumPy array:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Method 1) Using to_numpy() Method<\/strong><\/h3>\n\n\n\n<p><strong>The simplest method to convert a Pandas DataFrame to a NumPy array is by using the to_numpy() method. The to_numpy function is provided by the Pandas library<\/strong>.<\/p>\n\n\n\n<p>The following code will help you perform this conversion:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Create the DataFrame\ndata = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}\ndf = pd.DataFrame(data)\n\n# To convert this DataFrame to a NumPy array, we can use the to_numpy() method\narray = df.to_numpy()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91;&#91;1 4 7]\n &#91;2 5 8]\n &#91;3 6 9]]\n<\/code><\/pre>\n\n\n\n<p>Now, if we want to convert only a specific column to a NumPy Array we can do that by specifying the column. Let us see an example for that:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}\n\n# Create the DataFrame\ndf = pd.DataFrame(data)\n\n# Specify the columns of the df \narray = df[['A', 'B']].to_numpy()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91;&#91;1 4]\n &#91;2 5]\n &#91;3 6]]\n<\/code><\/pre>\n\n\n\n<p>We can also convert both rows and columns of a DataFrame to a NumPy Array. We can do this by using the <a href=\"https:\/\/favtutor.com\/articles\/pandas-iloc-function\/\">Pandas iloc<\/a> indexer. The following examples show that:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Make the DataFrame\ndata = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}\ndf = pd.DataFrame(data)\n\n# Using the iloc indexer with to_numpy() method\narray = df.iloc[[0, 1, 2], [0, 1, 2]].to_numpy()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91;&#91;1 4 7]\n &#91;2 5 8]\n &#91;3 6 9]]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Method 2) Using values Attribute<\/strong><\/h3>\n\n\n\n<p>Another method to convert a Pandas DataFrame to a NumPy array is by accessing the values attribute of the DataFrame. This is an easy and quick way for this type of conversion. Here is an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import  pandas as pd\n\n# Creating the DataFrame\ndata = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}\ndf = pd.DataFrame(data)\n\n# Using the values attribute\narray = df.values<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91;&#91;1 4 7]\n &#91;2 5 8]\n &#91;3 6 9]]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have explored different methods to convert a Pandas DataFrame to a NumPy array. The to_numpy() method, provided by Pandas, allows us to convert the entire DataFrame, a specific column, or both of the rows and columns to a NumPy array easily. conversion.\u00a0Another popular conversion is to <a href=\"https:\/\/favtutor.com\/articles\/convert-pandas-series-to-dataframe\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/convert-pandas-series-to-dataframe\/\">get a dataframe from a series<\/a>, which you must learn to crack technical interviews.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Pandas library in Python provides multi-data type analysis with lots of built-in classes and functions. The Numpy library on the other hand provides multi-dimensional analysis of numeric data. Both libraries provide a robust tool for two-dimensional data analysis. In this article, we will discuss the various methods we can use to convert a Pandas [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":1235,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[35],"tags":[37],"class_list":["post-1233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-pandas"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1233"}],"version-history":[{"count":3,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1233\/revisions"}],"predecessor-version":[{"id":1298,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1233\/revisions\/1298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1235"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}