I have a list of coordinates in the form
X | Y | Z |
---|---|---|
X1 | Y1 | Z (X1,Y1) |
X1 | Y2 | Z (X1,Y2) |
... | ... | ... |
X1 | YN | Z (X1,YN) |
X2 | Y1 | Z (X2,Y1) |
X2 | Y2 | Z (X2,Y1) |
... | ... | ... |
XN | YN | Z (XN,YN) |
I needed to do it this way because the function of Z was finicky.
Matplot lib needs the Z values in is a 2D array. I want to rearragne the table such that:
Y1 | Y2 | ... | YN | |
---|---|---|---|---|
X1 | X(X1,Y1) | Z(X1, Y2) | ... | Z(X1, YN) |
X2 | Z(X2, Y1) | Z(X2, Y2) | ... | Z(X2, YN) |
... | ... | ... | ... | ... |
XN | Z(XN, Y1) | Z(XN, Y2) | ... | Z(XN, YN) |
I've tried a few pivot options in Pandas, but no luck. Iterating through the table is proving unreliable.
I'm lost, please help.
Edit: `plotable_table = table.pivot(index = 'X', columns = 'Y', values = 'Z') returns something odd. Unfortunately table is a 3 x 230 and plotable_table is 23 x 142.... It should be 23 x 10
table is:
X Y Z
0 10000 0.0 0.0
1 10000 1000.0 -200.6
2 10000 2000.0 -401.2
3 10000 3000.0 -601.8
4 10000 4000.0 -802.4
.. ... ... ...
225 230000 115000.0 -9050.5
226 230000 138000.0 -26726.0
227 230000 161000.0 -56028.0
228 230000 184000.0 -66562.0
229 230000 207000.0 -77096.0
[230 rows x 3 columns]
plotable table is:
Y 0.0 1000.0 2000.0 ... 189000.0 198000.0 207000.0
X ...
10000 0.0 -200.6 -401.2 ... NaN NaN NaN
20000 0.0 NaN -401.2 ... NaN NaN NaN
30000 0.0 NaN NaN ... NaN NaN NaN
40000 0.0 NaN NaN ... NaN NaN NaN
50000 4070.0 NaN NaN ... NaN NaN NaN
60000 4884.0 NaN NaN ... NaN NaN NaN
70000 5698.0 NaN NaN ... NaN NaN NaN
80000 6512.0 NaN NaN ... NaN NaN NaN
90000 9846.0 NaN NaN ... NaN NaN NaN
100000 18230.0 NaN NaN ... NaN NaN NaN
110000 20053.0 NaN NaN ... NaN NaN NaN
120000 24768.0 NaN NaN ... NaN NaN NaN
130000 26832.0 NaN NaN ... NaN NaN NaN
140000 28896.0 NaN NaN ... NaN NaN NaN
150000 30960.0 NaN NaN ... NaN NaN NaN
160000 41184.0 NaN NaN ... NaN NaN NaN
170000 43758.0 NaN NaN ... NaN NaN NaN
180000 46332.0 NaN NaN ... NaN NaN NaN
190000 48906.0 NaN NaN ... NaN NaN NaN
200000 51480.0 NaN NaN ... NaN NaN NaN
210000 54054.0 NaN NaN ... -86562.0 NaN NaN
220000 73568.0 NaN NaN ... NaN -73744.0 NaN
230000 76912.0 NaN NaN ... NaN NaN -77096.0
[23 rows x 142 columns]
The answer for this is:
data = data.pivot(index='X', columns='Y', values='Z')
The dimensions of your pivot table depends of the unique values of X and Y. You should count how many distinct values you have for Y.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.