Say I have an array of numpy arrays df
and I want to list the pairs of consecutive rows? How do I do that?
I only know how to list each row one at a time and combine them into a list like this: z=[x for x in df]
. Then I'd do [np.vstack(z[i],z[i+1]) for i in range(len(z))]
.
I'm just wondering if there's a Pythonic way to do this instead.
My df is for example
array([[1., 1., 1., 0., 0., 0., 1., 0., 1.],
[0., 2., 0., 0., 6., 0., 0., 2., 0.],
[0., 2., 0., 0., 6., 0., 0., 2., 0.],
[0., 3., 4., 9., 1., 2., 2., 1., 8.],
[0., 0., 5., 0., 0., 0., 5., 9., 0.]])
And I'd like to return pairs of consecutive arrays at a time.
*** UPDATE
Okay so now I'm trying to return a dataframe where the columns are (first six entries in the row, the 8th entry, the 7th entry, the 9th entry, and the first six entries in the subsequent row).
When I do pd.DataFrame([[np.array([df1[1,:,:][j][:6],df1[1,:,:][j][7],df1[1,:,:][j][6],df1[1,:,:][j][8],df1[1,:,:][j+1][:6]])] for j in range(len(df1[1,:,:])-1)])
it looks fine and I get
0
0 [[1.0, 1.0, 1.0, 0.0, 0.0, 0.0], 0.0, 1.0, 1.0...
1 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 0.0, 0.0, 0.0...
2 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 0.0, 0.0, 0.0...
3 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 0.0, 0.0, 0.0...
4 [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 0.0, 0.0, 0.0...
.
However, when I do
pd.DataFrame([[np.array([df1[1,:,:][j][:6],df1[1,:,:][j][7],df1[1,:,:][j][6],df1[1,:,:][j][8],df1[1,:,:][j+1][:6]])] for j in range(len(df1[1,:,:])-1)],columns=['s','a','r','absorb','ns'])
(the only difference is that I provide column names now), I get the error
AssertionError: 5 columns passed, passed data had 1 columns.
How do I work around this? Thanks.