Numpy Array to Pandas Data Frame from XY Coordinates

I have a 2 dimensional numpy array:

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

      

How would I go about converting this to a pandas dataframe that would have an x โ€‹โ€‹coordinate, a y coordinate, and the corresponding array value at that index in a pandas dataframe like this:

x   y    val
0   0    1
0   1    4
0   2    7
1   0    2
1   1    5
1   2    8
...

      

+3


source to share


2 answers


With a stack and reset index:

df = pd.DataFrame(arr).stack().rename_axis(['y', 'x']).reset_index(name='val')
df

Out: 
   y  x  val
0  0  0    1
1  0  1    2
2  0  2    3
3  1  0    4
4  1  1    5
5  1  2    6
6  2  0    7
7  2  1    8
8  2  2    9

      



If order is very important:

df.sort_values(['x', 'y'])[['x', 'y', 'val']].reset_index(drop=True)
Out: 
   x  y  val
0  0  0    1
1  0  1    4
2  0  2    7
3  1  0    2
4  1  1    5
5  1  2    8
6  2  0    3
7  2  1    6
8  2  2    9

      

+5


source


Here's the NumPy method -



>>> arr
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> shp = arr.shape
>>> r,c = np.indices(shp)
>>> pd.DataFrame(np.c_[r.ravel(), c.ravel(), arr.ravel('F')], \
                                columns=((['x','y','val'])))

   x  y  val
0  0  0    1
1  0  1    4
2  0  2    7
3  1  0    2
4  1  1    5
5  1  2    8
6  2  0    3
7  2  1    6
8  2  2    9

      

+1


source







All Articles