1

I am trying to figure out a way to convert XYZ data that looks like this...

X   Y   Data

1   11  101

1   12  102

1   13  103 

1   14  104 

1   15  105 

2   11  101 

2   12  102

2   13  103 

2   14  104 

2   15  105 

3   11  101

3   12  102 

3   13  103 

3   14  104 

3   15  105 

4   11  101 

4   12  102 

4   13  103 

4   14  104

5   15  105 

5   11  101

5   12  102 

5   13  103 

5   14  104 

5   15  105 

to a grid format looking like this...

NAN 1    2   3   4   5

11  101 102 103 104 105

12  101 102 103 104 105

13  101 102 103 104 105

14  101 102 103 104 105

15  101 102 103 104 105

I have initially tried to use numpy module which feels like the right path for array manipulation, but I can't seem to figure out the solution. Any thoughts or help to point me in the right direction is appreciated.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Almidas
  • 379
  • 2
  • 6
  • 16

1 Answers1

0

You can use np.unique() to get the entries of X and Y, like so:

import numpy as np

X = np.array([1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5])
Y = np.array([11, 12, 13, 14, 15, 11, 12, 13, 14, 15, 11, 12, 13, 14, 15, 11, 12, 13, 14, 15, 11, 12, 13, 14, 15])

Data = np.array([101, 102, 103, 104, 105] * 5)

res = Data.reshape((5, 5))
xvals = np.insert(np.unique(X), 0, 0)
yvals = np.array([np.unique(Y)]).T

print(np.vstack(([xvals], np.hstack((yvals, res)))))

Note that I put 0 instead of NaN because NaN is only supported for floats in numpy (see here). If you really want NaN, I suggest you work with float arrays. Else, you can always put it to some value that will never occur (e.g. -1 if you are working with purely positive data). Or maybe it doesn't even matter what's in that field.

Community
  • 1
  • 1
greschd
  • 606
  • 8
  • 19