Home > Backend Development > Python Tutorial > How to Convert a Pandas DataFrame to a NumPy Array?

How to Convert a Pandas DataFrame to a NumPy Array?

Linda Hamilton
Release: 2024-12-14 05:28:14
Original
189 people have browsed it

How to Convert a Pandas DataFrame to a NumPy Array?

Convert pandas dataframe to NumPy array

How do I convert a pandas dataframe into a NumPy array?

Use df.to_numpy()

df.to_numpy() is the recommended method to convert a pandas dataframe into a NumPy array. It is defined on Index, Series, and DataFrame objects and provides a consistent way to extract the underlying NumPy array.

import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Convert the entire DataFrame
array = df.to_numpy()

# Convert specific columns
array = df[['A', 'C']].to_numpy()
Copy after login

The returned array is a view of the original DataFrame, so any modifications made to the array will be reflected in the DataFrame. To obtain a copy instead, use to_numpy(copy=True).

Preserving the dtypes

If you need to preserve the dtypes in the result, you can use DataFrame.to_records() instead of to_numpy().

record_array = df.to_records()
Copy after login

However, note that to_records() returns a rec.array, which is not a NumPy array. Instead, it is a structured array that is more akin to a pandas DataFrame. If you need a true NumPy array with dtypes preserved, you can use np.rec.fromrecords():

named_array = np.rec.fromrecords(v, names=v.columns.tolist())
Copy after login

The above is the detailed content of How to Convert a Pandas DataFrame to a NumPy Array?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template