How to keep column names when converting from pandas to numpy

前端 未结 4 1247
挽巷
挽巷 2021-02-20 08:03

According to this post, I should be able to access the names of columns in an ndarray as a.dtype.names

Howevever, if I convert a pandas DataFrame to an ndarray with df.a

4条回答
  •  被撕碎了的回忆
    2021-02-20 08:23

    Consider a DF as shown below:

    X = pd.DataFrame(dict(one=['Strawberry', 'Fields', 'Forever'], two=[1,2,3]))
    X
    

    Provide a list of tuples as data input to the structured array:

    arr_ip = [tuple(i) for i in X.as_matrix()]
    

    Ordered list of field names:

    dtyp = np.dtype(list(zip(X.dtypes.index, X.dtypes)))
    

    Here, X.dtypes.index gives you the column names and X.dtypes it's corresponding dtypes which are unified again into a list of tuples and fed as input to the dtype elements to be constructed.

    arr = np.array(arr_ip, dtype=dtyp)
    

    gives:

    arr
    # array([('Strawberry', 1), ('Fields', 2), ('Forever', 3)], 
    #       dtype=[('one', 'O'), ('two', '

    and

    arr.dtype.names
    # ('one', 'two')
    

提交回复
热议问题