Convert pandas dataframe to NumPy array

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

    Note: The .as_matrix() method used in this answer is deprecated. Pandas 0.23.4 warns:

    Method .as_matrix will be removed in a future version. Use .values instead.


    Pandas has something built in...

    numpy_matrix = df.as_matrix()
    

    gives

    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]])
    

提交回复
热议问题