JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

How to Convert a Pandas Dataframe to a NumPy Array

Author:JIYIK Last Updated:2025/05/02 Views:

We will introduce to_numpy()the method to pandas.Dataframeconvert a to NumPyan array, which is introduced in pandas v0.24.0, replacing the old .valuesmethod. We can define it on Index, Series, and DataFrameobjects to_numpy.

The old DataFrame.valueshas inconsistent behavior and we do not recommend using it according to the Pandas API documentation. However, if you are using an older version, we will explore examples of this method.

Another old method that is deprecated is DataFrame.as_matrix(), please don't use it!

We will also introduce another DataFrame.to_records()way to convert a given DataFrameinto NumPyan array of records using the method.


to_numpyMethod DataFrameconverts to NumPyan array

pandas.Dataframeis a two-dimensional tabular data structure with rows and columns. to_numpyThis data structure can be converted to NumPyan array using the method:

# python 3.x
import pandas as pd
import numpy as np

df = pd.DataFrame(data=np.random.randint(0, 10, (6, 4)), columns=["a", "b", "c", "d"])
nmp = df.to_numpy()
print(nmp)
print(type(nmp))

Output:

[[5 5 1 3]
 [1 6 6 0]
 [9 1 2 0]
 [9 3 5 3]
 [7 9 4 9]
 [8 1 8 9]]
<class 'numpy.ndarray'>

Dataframe.valuesThis can be achieved using the method in the following way :

# python 3.x
import pandas as pd
import numpy as np

df = pd.DataFrame(data=np.random.randint(0, 10, (6, 4)), columns=["a", "b", "c", "d"])
nmp = df.values
print(nmp)
print(type(nmp))

Output:

[[8 8 5 0]
 [1 7 7 5]
 [0 2 4 2]
 [6 8 0 7]
 [6 4 5 1]
 [1 8 4 7]]
<class 'numpy.ndarray'>

If we want to NumPyinclude in the array indexes, we need to Dataframe.valuesapply reset_index():

# python 3.x
import pandas as pd
import numpy as np

df = pd.DataFrame(data=np.random.randint(0, 10, (6, 4)), columns=["a", "b", "c", "d"])
nmp = df.reset_index().values
print(nmp)
print(type(nmp))

Output:

[[0 1 0 3 7]
 [1 8 2 5 1]
 [2 2 2 7 3]
 [3 3 4 3 7]
 [4 5 4 4 3]
 [5 2 9 7 6]]
<class 'numpy.ndarray'>

to_records()Method DataFrameconverts to NumPyan array of records

If you need this dtypes, then to_records()is the best choice. In terms of performance, to_numpy()it to_records()is almost the same as:

# python 3.x
import pandas as pd
import numpy as np

df = pd.DataFrame(data=np.random.randint(0, 10, (6, 4)), columns=["a", "b", "c", "d"])
nmp = df.to_records()
print(nmp)
print(type(nmp))

Output:

[(0, 0, 4, 6, 1) 
 (1, 3, 1, 7, 1) 
 (2, 9, 1, 6, 4) 
 (3, 1, 4, 6, 9)
 (4, 9, 1, 3, 9)
 (5, 2, 5, 7, 9)]
<class 'numpy.recarray'>

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to Convert DataFrame Column to String in Pandas

Publish Date:2025/05/02 Views:161 Category:Python

We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a

How to count the frequency of values in a Pandas DataFrame

Publish Date:2025/05/02 Views:84 Category:Python

Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().

How to get value from Pandas DataFrame cell

Publish Date:2025/05/02 Views:147 Category:Python

We'll look at using to get values ​​from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .

How to Add a Row to a Pandas DataFrame

Publish Date:2025/05/02 Views:127 Category:Python

Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to

How to change the order of Panas DataFrame columns

Publish Date:2025/05/02 Views:184 Category:Python

We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns

How to pretty print an entire Pandas Series/DataFrame

Publish Date:2025/05/02 Views:167 Category:Python

We will introduce various methods to pretty print the entire Pandas Series/DataFrame, such as option_context, set_option, and options.display. option_context Pretty Printing Pandas DataFrame We can option_context use with one or more option

Convert Pandas to CSV without index

Publish Date:2025/05/01 Views:159 Category:Python

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial