2

The problem: I have a 3-D Numpy Array:

X

X.shape: (1797, 2, 500)

z=X[..., -1]
print(len(z))
print(z.shape)
count = 0
for bot in z:
    print(bot)
    count+=1
    if count == 3: break

Above code yields following output:

1797
(1797, 2)
[23.293915 36.37388 ]
[21.594519 32.874397]
[27.29872  26.798382]

So, there are 1797 data points - each with a X and a Y coordinate and, there are 500 iterations of these 1797 points.

I want a DataFrame such that:

Index Column       |  X-coordinate  |  Y-coordinate
0                  |  X[0][0][0]    |  X[0][1][0]
0                  |  X[1][0][0]    |  X[1][1][0]
0                  |  X[2][0][0]    |  X[2][1][0]
('0') 1797 times
1                  |  X[0][0][1]    |  X[0][1][1]
1                  |  X[1][0][1]    |  X[1][1][1]
1                  |  X[2][0][1]    |  X[2][1][1]
('1' 1797 times)
.
.
.
and so on
till 500

I tried techniques mentioned here, but numpy/pandas is really escaping me:

  1. How To Convert a 3D Array To a Dataframe
  2. How to transform a 3d arrays into a dataframe in python
  3. Convert numpy array to pandas dataframe
  4. easy multidimensional numpy ndarray to pandas dataframe method?
  5. numpy rollaxis - how exactly does it work?

Please help me out. Hope I am adhering to the question-asking discipline.

raghavsikaria
  • 867
  • 17
  • 30
  • 1
    Question-asking discipline says: show what you tried and explain why it did not work. Here is the authoritative link: https://stackoverflow.com/help/how-to-ask – Mad Physicist Mar 31 '20 at 21:08
  • 1
    Hi @MadPhysicist, my apologies. Please let me update the question. – raghavsikaria Mar 31 '20 at 21:10
  • Once again, sincere apologies to the forum and @MadPhysicist, while I was working on preparing pointers you asked for, I got notifications with some responses to the question which have solved my problem. This was my first ever question and will really take care from next time onwards. – raghavsikaria Mar 31 '20 at 21:35

2 Answers2

1

Here's a solution with sample data:

a,b,c = X.shape
# in your case
# a,b,c = 1797, 500

pd.DataFrame(X.transpose(1,2,0).reshape(2,-1).T,
             index=np.repeat(np.arange(c),a),
             columns=['X_coord','Y_coord'] 
            )

Output:

   X_coord  Y_coord
0        0        3
0        6        9
0       12       15
0       18       21
1        1        4
1        7       10
1       13       16
1       19       22
2        2        5
2        8       11
2       14       17
2       20       23
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

Try this way:

index = np.concatenate([np.repeat([i], 1797) for i in range(500)])
df = pd.DataFrame(index=index)
df['X-coordinate'] = X[:, 0, :].T.reshape((-1))
df['Y-coordinate'] = X[:, 1, :].T.reshape((-1))
Bruno Mello
  • 4,448
  • 1
  • 9
  • 39