Convert Select Columns in Pandas Dataframe to Numpy Array

前端 未结 6 475
南方客
南方客 2020-12-13 02:05

I would like to convert everything but the first column of a pandas dataframe into a numpy array. For some reason using the columns= parameter of DataFram

6条回答
  •  别那么骄傲
    2020-12-13 02:07

    The columns parameter accepts a collection of column names. You're passing a list containing a dataframe with two rows:

    >>> [df[1:]]
    [  viz  a1_count  a1_mean  a1_std
    1   n         0      NaN     NaN
    2   n         2       51      50]
    >>> df.as_matrix(columns=[df[1:]])
    array([[ nan,  nan],
           [ nan,  nan],
           [ nan,  nan]])
    

    Instead, pass the column names you want:

    >>> df.columns[1:]
    Index(['a1_count', 'a1_mean', 'a1_std'], dtype='object')
    >>> df.as_matrix(columns=df.columns[1:])
    array([[  3.      ,   2.      ,   0.816497],
           [  0.      ,        nan,        nan],
           [  2.      ,  51.      ,  50.      ]])
    

提交回复
热议问题