Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy Array to Pandas Data Frame of X Y Coordinates

I have a two dimensional numpy array:

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

How would I go about converting this into a pandas data frame that would have the x coordinate, y coordinate, and corresponding array value at that index into a pandas data frame like this:

x   y    val
0   0    1
0   1    4
0   2    7
1   0    2
1   1    5
1   2    8
...
like image 655
Bryce Frank Avatar asked Dec 24 '22 17:12

Bryce Frank


2 Answers

With 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 ordering is 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
like image 97
ayhan Avatar answered Jan 02 '23 08:01

ayhan


Here's a 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
like image 43
Divakar Avatar answered Jan 02 '23 08:01

Divakar