Convert Select Columns in Pandas Dataframe to Numpy Array

前端 未结 6 469
南方客
南方客 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:21

    The fastest and easiest way is to use .as_matrix(). One short line:

    df.iloc[:,[1,2,3]].as_matrix()
    

    Gives:

    array([[3, 2, 0.816497],
       [0, 'NaN', 'NaN'],
       [2, 51, 50.0]], dtype=object)
    

    By using indices of the columns, you can use this code for any dataframe with different column names.

    Here are the steps for your example:

    import pandas as pd
    columns = ['viz', 'a1_count', 'a1_mean', 'a1_std']
    index = [0,1,2]
    vals = {'viz': ['n','n','n'], 'a1_count': [3,0,2], 'a1_mean': [2,'NaN', 51], 'a1_std': [0.816497, 'NaN', 50.000000]}
    df = pd.DataFrame(vals, columns=columns, index=index)
    

    Gives:

       viz  a1_count a1_mean    a1_std
    0   n         3       2  0.816497
    1   n         0     NaN       NaN
    2   n         2      51        50
    

    Then:

    x1 = df.iloc[:,[1,2,3]].as_matrix()
    

    Gives:

    array([[3, 2, 0.816497],
       [0, 'NaN', 'NaN'],
       [2, 51, 50.0]], dtype=object)
    

    Where x1 is numpy.ndarray.

提交回复
热议问题