convert pandas series AND dataframe objects to a numpy array

前端 未结 1 1863
囚心锁ツ
囚心锁ツ 2021-01-22 14:23

Series to Numpy Array:

I have a pandas series object that looks like the following:

s1 = pd.Series([0,1,2,3,4,5,6,7,8], index=[\'AB\', \'A         


        
相关标签:
1条回答
  • 2021-01-22 14:48

    IIUC, you may try numpy transpose and reshape

    df.values.T.reshape(-1,  int(dimension_len), int(dimension_len))
    
    Out[30]:
    array([[[ 0.,  1.,  2.],
            [ 3.,  4.,  5.],
            [ 6.,  7.,  8.]],
    
           [[nan, -2., nan],
            [ 2., nan, nan],
            [nan, nan, nan]],
    
           [[nan, nan,  4.],
            [nan, nan,  3.],
            [-4., -3., nan]]])
    
    0 讨论(0)
提交回复
热议问题