Convert pandas dataframe to NumPy array

前端 未结 15 2325
别那么骄傲
别那么骄傲 2020-11-21 23:57

I am interested in knowing how to convert a pandas dataframe into a NumPy array.

dataframe:

import numpy as np
import pandas as pd

index = [1, 2, 3,         


        
相关标签:
15条回答
  • 2020-11-22 00:54

    Two ways to convert the data-frame to its Numpy-array representation.

    • mah_np_array = df.as_matrix(columns=None)

    • mah_np_array = df.values

    Doc: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.as_matrix.html

    0 讨论(0)
  • 2020-11-22 00:55

    To convert a pandas dataframe (df) to a numpy ndarray, use this code:

    df.values
    
    array([[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]])
    
    0 讨论(0)
  • 2020-11-22 00:58

    I would just chain the DataFrame.reset_index() and DataFrame.values functions to get the Numpy representation of the dataframe, including the index:

    In [8]: df
    Out[8]: 
              A         B         C
    0 -0.982726  0.150726  0.691625
    1  0.617297 -0.471879  0.505547
    2  0.417123 -1.356803 -1.013499
    3 -0.166363 -0.957758  1.178659
    4 -0.164103  0.074516 -0.674325
    5 -0.340169 -0.293698  1.231791
    6 -1.062825  0.556273  1.508058
    7  0.959610  0.247539  0.091333
    
    [8 rows x 3 columns]
    
    In [9]: df.reset_index().values
    Out[9]:
    array([[ 0.        , -0.98272574,  0.150726  ,  0.69162512],
           [ 1.        ,  0.61729734, -0.47187926,  0.50554728],
           [ 2.        ,  0.4171228 , -1.35680324, -1.01349922],
           [ 3.        , -0.16636303, -0.95775849,  1.17865945],
           [ 4.        , -0.16410334,  0.0745164 , -0.67432474],
           [ 5.        , -0.34016865, -0.29369841,  1.23179064],
           [ 6.        , -1.06282542,  0.55627285,  1.50805754],
           [ 7.        ,  0.95961001,  0.24753911,  0.09133339]])
    

    To get the dtypes we'd need to transform this ndarray into a structured array using view:

    In [10]: df.reset_index().values.ravel().view(dtype=[('index', int), ('A', float), ('B', float), ('C', float)])
    Out[10]:
    array([( 0, -0.98272574,  0.150726  ,  0.69162512),
           ( 1,  0.61729734, -0.47187926,  0.50554728),
           ( 2,  0.4171228 , -1.35680324, -1.01349922),
           ( 3, -0.16636303, -0.95775849,  1.17865945),
           ( 4, -0.16410334,  0.0745164 , -0.67432474),
           ( 5, -0.34016865, -0.29369841,  1.23179064),
           ( 6, -1.06282542,  0.55627285,  1.50805754),
           ( 7,  0.95961001,  0.24753911,  0.09133339),
           dtype=[('index', '<i8'), ('A', '<f8'), ('B', '<f8'), ('C', '<f8')])
    
    0 讨论(0)
提交回复
热议问题