convert pandas series AND dataframe objects to a n

2020-04-16 17:37发布

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', 'AC','AD', 'BA','BB','BC','CA','CB','CC'])

I want to convert this series to a numpy array as follows:

series_size = s1.size
dimension_len = np.sqrt(series_size) 
**Note: series_size will always have an integer sqrt

The dimension_len will determine the size of each of the dimensions in the desired 2 dimensional array.

In the above series object, the dimension_len = 3 so the desired numpy array will be a 3 x 3 array as follows:

np.array([[0, 1, 2], 
[3, 4, 5],
[6,7, 8]])

Dataframe to Numpy Array:

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

s1 = pd.Series([0,1,2,3,4,5,6,7,8], index=['AA', 'AB','AC', 'BA','BB','BC','CA','CB','CC'])
s2 = pd.Series([-2,2], index=['AB','BA'])
s3 = pd.Series([4,3,-3,-4], index=['AC','BC', 'CB','CA'])

df = pd.concat([s1, s2, s3], axis=1)

max_size = max(s1.size, s2.size, s3.size)

dimension_len = np.sqrt(max_size)
num_columns = len(df.columns)
**Note: max_size will always have an integer sqrt

The resulting numpy array will be determined by the following information:

num_columns = determines number of dimensions of the array dimension_len = determines the size of each dimension

In the above example the desired numpy array will be 3 x 3 x 3 (num_columns = 3 and dimension_len = 3)

As well the first column of df will become DESIRED_ARRAY[0], the second column of df will become DESIRED_ARRAY[1], the third column of df will become DESIRED_ARRAY[2] and so on...

The desired array I want looks like:

np.array([[[0, 1, 2], 
[3, 4, 5],
[6, 7, 8]],

[[np.nan,-2, np.nan],
[2, np.nan, np.nan],
[np.nan, np.nan, np.nan]],

[[np.nan,np.nan, 4],
[np.nan, np.nan, 3],
[-4, -3, np.nan]],
])

1条回答
三岁会撩人
2楼-- · 2020-04-16 17:55

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]]])
查看更多
登录 后发表回答