# How to Convert a DataFrame to a Matrix in Python [4 ways]

Do you want to convert a DataFrame to a matrix? In this Pandas tutorial, I will tell you “How to convert a DataFrame to a Matrix in Python” using different methods with some examples.

To convert a DataFrame to a matrix in Python, use the values attribute for a quick transformation, or to_numpy() for more control and options. For further flexibility, especially with data types, NumPy’s asarray() function is ideal. Alternatively, combine values with the astype() method to specify data types explicitly, ensuring your matrix is perfectly tailored to our analytical needs.

## Convert a DataFrame to a Matrix in Python

There are four different ways to convert a DataFrame to a matrix in Python:

1. Using values Attribute
2. Using to_numpy() Method
3. Using asarray() Function
4. Using values with Specific Data Types

Let’s see them one by one using some illustrative examples:

### 1. DataFrame to matrix Python using values attribute

This is the simplest approach where we use the values attribute of the Pandas DataFrame to directly convert it into a NumPy array in Python, effectively turning it into a matrix.

Here is an instance, of converting a DataFrame to a matrix in Python using values attribute:

``````import pandas as pd

df = pd.DataFrame({
'New York': [32, 35, 42],
'Los Angeles': [58, 61, 63],
'Chicago': [24, 28, 38]
})

matrix = df.values
print(matrix)``````

Output:

``````[[32 58 24]
[35 61 28]
[42 63 38]]``````

After the code has been implemented in the Pycharm editor, the resulting screenshot is provided below.

### 2. Python DataFrame to matrix using to_numpy() method

The to_numpy() method of a DataFrame in Pandas, explicitly converts it into a NumPy array in Python. This method offers additional options such as specifying the data type, making it a bit more flexible than using the values attribute.

READ:  How to Create Pandas Crosstab Percentage in Python? [3 Examples]

Here is an example to convert a DataFrame to a matrix in Python using the to_numpy() method:

``````import pandas as pd

df = pd.DataFrame({
'New York City': [8.4, 8.3, 8.2],
'Los Angeles': [4.0, 4.1, 4.2],
'Chicago': [2.7, 2.7, 2.6]
})

matrix = df.to_numpy()
print(matrix)``````

Output:

``````[[8.4 4.  2.7]
[8.3 4.1 2.7]
[8.2 4.2 2.6]]``````

The screenshot below presents the output obtained after the code execution in the Pycharm editor.

### 3. Convert DataFrame to matrix Python using asarray() function

This method uses the asarray() function from NumPy, which converts the input into an array in Python. It’s beneficial when we need more control over the data type and memory allocation, as it does not copy the data if it’s already an ndarray.

Here is a demonstration, of how to convert a DataFrame to a matrix in Python using the asarray() function:

``````import pandas as pd
import numpy as np

df = pd.DataFrame({
'California': [163696, 163695, 163697],
'Texas': [268596, 268598, 268597],
'Florida': [65758, 65757, 65759]
})
matrix = np.asarray(df)
print(matrix)``````

Output:

``````[[163696 268596  65758]
[163695 268598  65757]
[163697 268597  65759]]``````

Displayed below is a screenshot that reveals the output after the code has been implemented in the Pycharm editor.

### 4. Python convert DataFrame to matrix using values with specific datatypes

Combining the values attribute with the astype() method in Python allows for converting a Pandas dataframe to a matrix with a specific data type. It’s useful when data type consistency is crucial for further processing or analysis.

Here is an example of converting a DataFrame to a matrix in Python using values attribute with the astype() method:

``````import pandas as pd

df = pd.DataFrame({
'California': [3.2],
'Texas': [1.9],
'New York': [1.7]
})
matrix = df.values.astype(float)
print(matrix)``````

Output:

``[[3.2 1.9 1.7]]``

Upon running the code in Pycharm, the resulting output is displayed in the screenshot below.

## Conclusion

Understanding the various methods as the values attribute, to_numpy() method, NumPy’s asarray() function, and the combination of values with specific data types, provides a comprehensive toolkit for efficiently converting a DataFrame to a matrix in Python.

Each method offers unique benefits, catering to different data processing requirements and scenarios.

You may also like to read: