Home >Backend Development >Python Tutorial >How can I convert a Pandas DataFrame with missing values into a NumPy array using `df.to_numpy()` and preserve data types?

How can I convert a Pandas DataFrame with missing values into a NumPy array using `df.to_numpy()` and preserve data types?

Linda Hamilton
Linda HamiltonOriginal
2024-11-06 03:57:02837browse

How can I convert a Pandas DataFrame with missing values into a NumPy array using `df.to_numpy()` and preserve data types?

Convert Pandas Dataframe with Missing Values to NumPy Array

Using df.to_numpy()

To convert a Pandas dataframe with missing values into a NumPy array with np.nan representing missing values, use the df.to_numpy() method. It provides a consistent and reliable way to obtain NumPy arrays from both dataframes and index/series objects.

<code class="python">import numpy as np
import pandas as pd

df = pd.DataFrame({
    "A": [np.nan, np.nan, np.nan, 0.1, 0.1, 0.1, 0.1],
    "B": [0.2, np.nan, 0.2, 0.2, 0.2, np.nan, np.nan],
    "C": [np.nan, 0.5, 0.5, np.nan, 0.5, 0.5, np.nan],
}, index=[1, 2, 3, 4, 5, 6, 7])

np_array = df.to_numpy()
print(np_array)</code>

This will output a NumPy array with missing values represented as np.nan:

[[ nan  0.2  nan]
 [ nan  nan  0.5]
 [ nan  0.2  0.5]
 [ 0.1  0.2  nan]
 [ 0.1  0.2  0.5]
 [ 0.1  nan  0.5]
 [ 0.1  nan  nan]]

Preserving Data Types

To preserve data types in the NumPy array, use the np.rec.fromrecords() function:

<code class="python">v = df.reset_index()
np_array_dtypes = np.rec.fromrecords(v, names=v.columns.tolist())
print(np_array_dtypes)</code>

This will output a NumPy array with the original data types preserved as follows:

rec.array([('1', 1, 0.2, 0.5), ('2', 2, np.nan, 0.5), ('3', 3, 0.2, 0.5),
           ('4', 4, 0.2, np.nan), ('5', 5, 0.2, 0.5), ('6', 6, np.nan, 0.5),
           ('7', 7, np.nan, np.nan)],
          dtype=[('index', '<U1'), ('A', '<f8'), ('B', '<f8'), ('C', '<f8')])

The above is the detailed content of How can I convert a Pandas DataFrame with missing values into a NumPy array using `df.to_numpy()` and preserve data types?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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