Convert pandas dataframe to NumPy array

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

    A simple way to convert dataframe to numpy array:

    import pandas as pd
    df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
    df_to_array = df.to_numpy()
    array([[1, 3],
       [2, 4]])
    

    Use of to_numpy is encouraged to preserve consistency.

    Reference: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html

提交回复
热议问题