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?
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]]
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!