Convert pandas dataframe to NumPy array

前端 未结 15 2362
别那么骄傲
别那么骄傲 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:34

    Try this:

    np.array(df) 
    
    array([['ID', nan, nan, nan],
       ['1', nan, 0.2, nan],
       ['2', nan, nan, 0.5],
       ['3', nan, 0.2, 0.5],
       ['4', 0.1, 0.2, nan],
       ['5', 0.1, 0.2, 0.5],
       ['6', 0.1, nan, 0.5],
       ['7', 0.1, nan, nan]], dtype=object)
    

    Some more information at: [https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html] Valid for numpy 1.16.5 and pandas 0.25.2.

提交回复
热议问题