Convert Pandas DataFrame to NumPy Array (With Examples)


You can use the following syntax to convert a pandas DataFrame to a NumPy array:

df.to_numpy()

The following examples show how to use this syntax in practice.

Example 1: Convert DataFrame with Same Data Types

The following code shows how to convert a pandas DataFrame to a NumPy array when each of the columns in the DataFrame is the same data type:

import pandas as pd

#create data frame
df1 = pd.DataFrame({'rebounds': [7, 7, 8, 13, 7, 4],
                    'points': [5, 7, 7, 9, 12, 9],
                    'assists': [11, 8, 10, 6, 6, 5]})

#view data frame
print(df1)

   rebounds  points  assists
0         7       5       11
1         7       7        8
2         8       7       10
3        13       9        6
4         7      12        6
5         4       9        5

#convert DataFrame to NumPy array
new = df1.to_numpy()

#view NumPy array
print(new)

[[ 7  5 11]
 [ 7  7  8]
 [ 8  7 10]
 [13  9  6]
 [ 7 12  6]
 [ 4  9  5]]

#confirm that new is a NumPy array
print(type(new))

<class 'numpy.ndarray'> 

#view data type
print(new.dtype)

int64

The Numpy array has a data type of int64 since each column in the original pandas DataFrame was an integer.

Example 2: Convert DataFrame with Mixed Data Types

The following code shows how to convert a pandas DataFrame to a NumPy array when the columns in the DataFrame are not all the same data type:

import pandas as pd

#create data frame
df2 = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E', 'F'],
                    'points': [5, 7, 7, 9, 12, 9],
                    'assists': [11, 8, 10, 6, 6, 5]})

#view data frame
print(df2)

  player  points  assists
0      A       5       11
1      B       7        8
2      C       7       10
3      D       9        6
4      E      12        6
5      F       9        5

#convert DataFrame to NumPy array
new = df2.to_numpy()

#view NumPy array
print(new)

[['A' 5 11]
 ['B' 7 8]
 ['C' 7 10]
 ['D' 9 6]
 ['E' 12 6]
 ['F' 9 5]]

#confirm that new is a NumPy array
print(type(new))

<class 'numpy.ndarray'> 

#view data type
print(new.dtype)

object

The Numpy array has a data type of object since not every column in the original pandas DataFrame was the same data type.

Example 3: Convert DataFrame & Set NA Values

The following code shows how to convert a pandas DataFrame to a NumPy array and specify the values to be set for any NA values in the original DataFrame:

import pandas as pd

#create data frame
df3 = pd.DataFrame({'player': ['A', 'B', pd.NA, 'D', 'E', 'F'],
                    'points': [5, 7, pd.NA, 9, pd.NA, 9],
                    'assists': [11, 8, 10, 6, 6, 5]})

#view data frame
print(df3)

  player points  assists
0      A      5       11
1      B      7        8
2   <NA>   <NA>       10
3      D      9        6
4      E   <NA>        6
5      F      9        5

#convert DataFrame to NumPy array
new = df3.to_numpy(na_value='none')

#view NumPy array
print(new)

[['A' 5 11]
 ['B' 7 8]
 ['none' 'none' 10]
 ['D' 9 6]
 ['E' 'none' 6]
 ['F' 9 5]]

#confirm that new is a NumPy array
print(type(new))

<class 'numpy.ndarray'> 

#view data type
print(new.dtype)

object

Additional Resources

How to Create a Pandas DataFrame from a NumPy Array
How to Convert a List to a DataFrame in Pandas
How to Convert a DataFrame to List in Pandas

Leave a Reply

Your email address will not be published.