Suppose my dataframe has 750 rows in a column and I want to convert that column to an image array of (20,20) numpy array. How to do that?
EDIT1: I want to use the array for ax.contourf (x,y,z) as z. I got x,y by doing x,y=np.meshgrid(df3.x,df.y) now I want to convert another column to an (n,n) array to vary the colors inside the contours using the z parameter.
EDIT2:x,y,z Data to plot contour plot
Advertisement
Answer
Given the fact that you have x and y as columns already, I would use pivot_table like this :
df = pd.DataFrame( columns=["z", "x", "y"], data=[ [-0.00222, 38, 46.1], [-0.00374, 30.5, 68.4], [0.001043, 36, 86.9], [0.003473, 52.9, 95.3], [0.001382, 50.6, 80.3], [-0.00486, 37, 92.7], [-0.00016, 29.8, 69.5], [0.001368, 34.7, 27.2], [-0.0016, 37.5, 63], [-0.00181, 45.5, 92], [0.003635, 53.6, 82.4], [-0.00363, 46.7, 91.1], [0.002253, 54.1, 97.8], [-0.00089, 43.8, 87], [-0.00115, 41, 62.7], [-0.001, 47.5, 23.2], [0.001332, 61.2, 4.7], [-0.00361, 45.9, 13.9], [0, 47.4, 32], [0, 54.1, 66.6], [0.000891, 53.2, 7.3], [-0.00133, 56.1, 15.8], [-0.00045, 43.6, 19.3], [0.004365, 68.8, 94.2], [-0.00355, 71.1, 76.1], [0, 71.3, 64.8], [-0.00415, 65.7, 59.4], [0.003881, 72.3, 47], [-0.00727, 54.7, 68.4], [0.001848, 63.3, 81.3], [0.002518, 69.7, 93.8], [-0.00252, 65.2, 84], [-0.0025, 55.8, 67.5], [0.004365, 68.2, 94.5], [0.008311, 76.4, 85.4], [0.000208, 35.4, 70.5], [-0.00021, 45, 92.7], [-0.00189, 32, 75.5], [0.000219, 32.3, 38.3], [0.001522, 33.4, 61.7], [0, 37.6, 37.1], [0.0016, 47.6, 32.5], [0.000595, 59.1, 7.1], [0, 63.1, 5], [0.00212, 55.3, 26.5], [0.002495, 64.7, 27.4], [0.00197, 79.4, 24.2], [0.006561, 75.2, 38.9], [0.011262, 82.3, 53.3], [0.019798, 88.7, 25.6], [-0.0198, 78.9, 45.2], [0.000602, 22.6, 74.7], [-0.00082, 29, 93.5], [0.002707, 43.8, 95], [0.000497, 27.7, 45.4], [-0.00131, 19.3, 28.7], [0.000152, 19.2, 38.3], [-0.00015, 23.5, 64.5], [0.001311, 31.7, 41.9], [0.004164, 54.8, 28.4], [-0.00185, 57.8, 32], [0.004771, 71.1, 69.3], [0.010728, 87.7, 92.3], [-0.0063, 81.6, 71.6], [0.079499, 84.6, 52.1], [-0.07294, 78.2, 52.1], [-0.00656, 80.6, 64.8], [0, 70.7, 67.2], [0.00541, 76.1, 75.5], [0.003548, 81.6, 93.2], [-0.00355, 74.9, 84.8], [-0.00016, 68.6, 60], [0.000164, 70, 17.2], [0.005518, 78.8, 16.3], [-0.00466, 64, 27.7], [0.000619, 66.5, 46], [-0.00552, 71.6, 77.9], [-0.00027, 72, 50.7], [0.002583, 46, 87.2], [0.007159, 69.6, 75.6], [0.005518, 77.1, 84.9], [-0.00729, 79.4, 92.4], [0.00197, 75.2, 75], [-0.00011, 74.7, 19.3], [0.011137, 90.9, 22.6], [0.006561, 77.8, 41.2], [-0.00186, 14.3, 38.6], [0.000497, 28, 61.2], [0.001303, 38.9, 55.7], [0.002049, 52.4, 41.8], [-0.00205, 41.9, 51.3], [-0.00055, 46.7, 70.2], [0.004365, 71.8, 94.5], [-0.00477, 65.8, 67.8], [0.00541, 75.5, 84.2], [-0.00541, 74.5, 72.2], [0.00541, 77.8, 85.7], [-0.00568, 72, 54.5], [0.001221, 46.9, 3.5], [0.003635, 58.2, 14.9], [0.000446, 45.2, 34], [0.004771, 68.4, 37.4], [0, 71.3, 55.1], [0, 67.6, 61.8], [0.022626, 94.2, 84.6], [-0.0026, 45.8, 69.8], [0.002565, 55.7, 74.9], [0.007159, 68, 76.1], [-0.00027, 71.1, 38.3], [0.019798, 91.3, 70.7], [0, 92.6, 92.4], [-0.01073, 74.6, 74.6], [0.005318, 87.2, 92.6], [-0.0063, 79.8, 70.5], [0.017925, 98.1, 78], [0.000668, 34.4, 91.2], [0, 36.2, 92.9], [0.000885, 38.2, 75.9], [-0.00094, 21.7, 47], [0.000939, 30.2, 31.3], [-0.00088, 37.9, 6.2], [-0.00016, 27.4, 36.7], [-0.00048, 39.8, 87.2], [0.005303, 63.2, 95.9], [0.001901, 61.8, 70.5], [0.000652, 22, 89.1], [0.003699, 33.6, 80.3], [0.005125, 60.3, 58.1], [0, 63.1, 38.5], [0, 65, 60.2], [0, 60.9, 39.1], [0.004152, 67.7, 27.1], [0.003441, 77, 9.7], [-0.00344, 74.6, 28], [0, 70.9, 49.7], [0.012118, 90.9, 23.8], [0.000366, 25.2, 93.3], [-0.00212, 58, 94.4], [-0.00225, 42.4, 93.5], [0.003635, 52, 85.7], [0.001332, 58.7, 94.5], [-0.00133, 53.9, 84.6], [-0.00388, 58.9, 45.1], [0.006956, 67, 48.2], [-0.007, 51.9, 67.8], [-0.00416, 39.1, 29.8], [-0.00253, 19.8, 44], [-0.00044, 45, 30.4], [-0.001, 45.9, 77.4], [0.002565, 53.1, 66.8], [-0.00416, 41.5, 35.2], [-0.00055, 46.9, 66], [0.000595, 64.1, 96], [0.001901, 61.2, 68.3], [-0.00252, 62.9, 9.4], [0, 73.4, 7.1], [0.025256, 94.3, 13.3], [-0.01084, 72.3, 16.4], [0.010991, 75.2, 56.3], [0.010999, 89.2, 87.5], [0.000164, 72.5, 80.4], [0.000107, 72, 69.6], [-0.00027, 73, 38.6], [0.002273, 56.7, 83.3], [-0.00035, 23.4, 77.6], [0.000353, 29.8, 87.5], [0.002707, 44, 95], [-0.00034, 34.3, 84.5], [-0.00122, 39.4, 59.7], [0.001219, 47.9, 77.1], [0.001001, 42.4, 58.1], [-0.00055, 43.9, 31.4], [0.000555, 45.1, 55.7], [0.003857, 58.6, 75.6], [0.002518, 67.5, 96.5], [-0.00059, 55.8, 73.7], [-0.00201, 49.5, 56.3], [-0.00078, 54, 17], [0.004152, 72, 66.5], [0.003441, 78.9, 91.4], [-0.00355, 70.4, 79.8], [-0.00027, 69.2, 61.7], [0.000271, 68.3, 36.2], [0.003441, 78.8, 11.2], [0, 82.8, 15.4], [-0.00098, 75.9, 33.5], [0, 75.2, 67.5], [0.012118, 90.5, 87], [0, 42.1, 10.4], [0.001808, 48.4, 31.4], [0.007266, 69.4, 26.6], [0.003548, 78.6, 6.2], [0.000989, 80.3, 28.1], [-0.00443, 74.8, 28.9], [0.001417, 10.9, 38.6], [0, 9.2, 46.3], [0.000497, 32.9, 49.4], [-0.0005, 8.6, 48.1], [0.000277, 28.1, 32.2], [-0.00232, 12.5, 23.3], [0.000991, 30.2, 3.5], [0.000818, 30.7, 18.7], [-0.00021, 43.8, 92.9], [0.000479, 39.6, 54.9], [0.00222, 48.8, 46], [-3.93E-05, 53.7, 29.9], [-0.00166, 35.1, 50.7], [-0.00116, 17.8, 45.1], [0.002461, 33.6, 44], [-0.00014, 46.2, 4], [0.001382, 54.5, 19.6], [3.93E-05, 54.5, 50.9], [0.001251, 73.6, 92.4], [0.000989, 75.2, 68.1], [-0.01036, 75.6, 94.4], [-0.00252, 66.5, 80.3], [0.005419, 69.7, 66.3], [0, 74.4, 74.4], [0, 70.9, 66.3], [0, 72.5, 72.5], [-0.00027, 67.6, 55.2], [0.016548, 90, 87.2], [0.067381, 86.9, 60.2], [-0.00656, 79.2, 71.6], [-0.0047, 73.7, 52.1], [0.005681, 78.1, 19.1], [0.006561, 78.2, 46.3], [-0.00656, 77.2, 69.3], [-0.0047, 72, 58.7], [-0.0045, 66, 67.4], [-0.00246, 53, 51.3], [0.007227, 66.9, 66.2], [0.003441, 79.8, 90.5], [-0.00016, 72, 54.5], [0.019798, 86.4, 29.5], [0.009515, 97.7, 25.6], [0.016818, 89.1, 76.8], [-0.01212, 80.7, 68.6], [0.013629, 85.6, 96.9], [-0.00729, 81, 96], [-0.00355, 71, 82.1], [-0.00011, 70.4, 24.5], [0.005518, 76.7, 14.9], [-0.00027, 71, 45.2], [0.005681, 79, 85.4], [-0.00568, 72.5, 53.6], [0, 72.5, 41.9], [0, 29.7, 66], [0, 31.7, 72.6], [0, 29.7, 64.2], [0.00222, 44.1, 44.9], [-0.0027, 37.8, 17.8], [0.000336, 44.3, 5], [-0.00034, 37.6, 16.4], [0.000336, 44.4, 12.1], [-0.00024, 29.9, 88.2], [0.000589, 23.2, 70.2], [0.001522, 33.9, 42.8], [-0.0006, 5.7, 45.7], [0.000602, 23, 37.1], [-0.00024, 25.9, 4.3], [-0.0042, 40.7, 68], [0.001154, 43.4, 87], [0.000891, 55.2, 93.5], [-0.00225, 42, 88.7], [-0.00138, 31.2, 74.7], [-0.00094, 16.8, 49.4], [0.002526, 41.4, 72.9], [0.000891, 55.9, 94.5], [-0.00133, 53, 75.3], [0.010668, 77.5, 95.7], [0.000989, 76.1, 68.1], [0.019798, 87.9, 30.8], [0.003771, 72.5, 96.5], [0.002253, 54.5, 93.2], [-0.00225, 43.3, 92.7], [-0.00271, 27.4, 91.7], [0.001051, 14.2, 54], [0.003464, 43.5, 67.1], [0.000555, 47.1, 57.5], [0.00201, 56.5, 72.5], [0.00187, 73.4, 95.6], [0.008311, 75.8, 77.9], [-0.00541, 67.2, 69.9], [-0.00027, 73.9, 54.6], [-0.01126, 71.8, 57], [0.004701, 81.4, 34.4], [0.000232, 9.2, 40.6], [0.000277, 26.3, 26.8], [-0.00075, 4.1, 51.5], [-0.00062, 8.5, 83.6], [0.002706, 46.6, 93.9], [0.003881, 68.7, 58.8], [0, 74.6, 47.5], [0, 70.7, 47.5], [-0.00149, 30.1, 90.6], [0.002097, 36.6, 65.4], [6.53E-05, 34.2, 30.3], [0.002044, 54.9, 4.1], [0, 54, 10.7], [0.004771, 68.9, 73.8], [0.00541, 82, 82.7], [-0.00098, 75.8, 64.2], [0, 79.5, 74.9], [-0.00552, 74.9, 75.9], [0.016944, 92.2, 85.5], [-0.00634, 70, 92.4], [0, 73.7, 96.2], [-0.00377, 61.8, 96.2], [0.00212, 53.7, 69], [0.002495, 59.7, 66.9], [0, 74, 83.4], [0.005518, 77.8, 86.3], [0.003548, 80.9, 93.6], [0, 79.1, 51.9], [-0.01126, 73.6, 55.5], [-0.00733, 75, 93.3], [-0.00344, 74.4, 71.3], [-0.00477, 66.3, 71], [0.004664, 68.1, 75.9], [0.000107, 69.9, 65.6], [-0.00027, 74.3, 55.7], [0, 66.7, 93], [0.002901, 68.1, 72.3], [0.00443, 75.2, 74.9], [0, 72.8, 92.3], [0.006342, 77.6, 95.6], [-0.00634, 70, 88.1], [-0.00016, 68.6, 57.8], [-0.00062, 66.4, 35.5], [0.000271, 73, 68.7], [-0.00388, 65.7, 53.7], [0.007592, 81.5, 11.3], [-0.01655, 74.3, 29.3], [-0.00388, 61.3, 53.9], [0.003881, 69, 49.9], [0.000271, 74.3, 64.5], [0.012118, 88.9, 86.1], [0.002565, 50.1, 66], [0.004326, 71.7, 7.1], [-0.0047, 71.3, 41.5], [0.004701, 79.3, 64.1], [-0.00843, 92.7, 76.2], [0.001253, 59.4, 80.3], [0, 56, 33.7], [0.000595, 65.4, 5.3], [0.001253, 61.5, 14.2], [0.001267, 65.2, 37.9], [0.003548, 77.4, 93.2], [-0.00355, 72.7, 85.4], [-0.00016, 70.7, 54.2], [0.026359, 91.4, 72.9], [-0.00729, 76.8, 6.1], [-0.00022, 29.6, 70.2], [0.001522, 36.5, 61.8], [0.011496, 67, 37.4], [-0.00477, 64.4, 26.8], [-0.00027, 74.5, 39.8], [0.019798, 90.3, 68.6], [0.000367, 14.6, 69], [0.001327, 28.8, 63.3], [0.001043, 37, 78.9], [0.000336, 42.2, 95.3], [-0.00189, 27.2, 85.1], [0.00222, 46.6, 56.6], [0.001328, 26.1, 93.8], [0.002707, 42.3, 92.3], [0.00011, 44.9, 38.5], [0.0045, 67.6, 38.8], [0, 62, 45.7], [0.007592, 75.3, 91.2], [0, 18.8, 55.4], [-0.00039, 13.4, 68.1], [-0.00014, 46.5, 89.7], [0, 42.4, 89.1], [0.002253, 53.8, 95], [0.005318, 85.7, 89.4], [-0.01212, 75.1, 66.3], [0.005681, 80.7, 14.6], [-0.00098, 76.1, 31.3], [-0.00027, 67.3, 56.9], [0.003712, 82.5, 87.8], [-0.01212, 80.2, 70.4], [-0.00541, 70.1, 74.4], [0.00541, 79.9, 85.8], [0, 75.2, 75.9], [-0.00098, 79.7, 72.9], [-0.00443, 72.1, 72.6], [0.00443, 76.6, 74.7], [-0.00443, 70.6, 73.5], [0.00443, 78.7, 65], [-0.0047, 72.7, 54.5], [0.003712, 82.8, 11.5], [-0.01655, 74.7, 29.8], [-0.00027, 73.3, 41.9], [0, 74.2, 57.6], [0.016548, 84.6, 86.1], [-0.01665, 74.3, 75], [0.004537, 80.3, 71.4], [-0.00116, 27.8, 47.3], [0, 39.1, 93.5], [0.003473, 53.5, 96], [-0.00122, 40.4, 91.1], [-0.00122, 34.4, 51], [0.002109, 52.5, 5.3], [0.00212, 52.7, 25.7], [0, 55.6, 36.7], [0.006996, 67.1, 43.9], [-0.00388, 62.7, 45.4], [0.001251, 74.1, 90.6], [0, 73.9, 63.3], [0, 70.7, 67.2], [-0.00027, 68.1, 44.3], [0, 65, 60.9], [0.004045, 73.5, 83.6], [0.00443, 81.6, 68.9], [1.33E-05, 22.2, 78], [0.010728, 85.8, 87.8], [-0.01665, 72.3, 78.6], [-0.00011, 72.6, 13.7], [0.00201, 51.7, 65.3], [-0.00256, 45.9, 68.8], [0.001848, 59.7, 78.6], [0.005419, 68.6, 73.2], [-0.00656, 78, 36.4], [0.000981, 80.4, 87.2], [-0.00541, 74.7, 71.3], [-0.00308, 87.4, 92.6], [-0.00532, 76.6, 85.8], [0.005318, 86.8, 93.2], [-0.01084, 72.8, 76.5], [0.004701, 81.8, 29.6], [0.006561, 81.9, 47.8], [-0.00089, 46.9, 79.8], [-0.00122, 39.3, 45.2], [0.00222, 46.3, 59.6], [0.001272, 55.6, 87.3], [0.002715, 64.9, 96.2], [-0.00125, 64, 91.4], [0.001253, 65.4, 78.9], [-0.00125, 63, 87.9], [-0.00059, 54.7, 73.4], [-0.00074, 54, 16.6], [0.001332, 60.6, 6.5], [0.000619, 64.9, 59.6], [0.001251, 72.3, 89.1], [0.011137, 85.4, 84.6], [0.001332, 66.6, 93.6], [-0.00252, 64.2, 86.3], [0.005312, 74.1, 77.6], [-0.00531, 66, 77.1], [0.002518, 70.1, 93.3], [-0.00252, 64.5, 83.6], [-0.00185, 54.7, 65.5], [-3.93E-05, 51.5, 35.2], [0.000595, 60.9, 4.7], [-0.00059, 58.3, 30.5], [-0.00415, 63, 52], [0.004664, 67.1, 77.4], [0.005518, 83, 87.3], [-0.0198, 79.4, 60.3], [0.202542, 97, 44.5], [0.000367, 10.1, 36.7], [-0.00037, 7.9, 49.3], [0, 29.8, 48.1], [0.001043, 37.5, 80], [0, 34.4, 54.2], [-0.00246, 24.3, 58.2], [0.002461, 35.9, 58.1], [-0.00048, 33.5, 86], [-0.00088, 41.6, 95.3], [0, 36, 89.6], [0.00305, 55.7, 56.9], [-0.0027, 37.1, 13.7], [0.003473, 54.3, 4.1], [0.000738, 56, 37.3], [3.93E-05, 53.8, 59.7], [0.00187, 74.6, 90.2], [0, 75.9, 66.3], [0.012118, 87.2, 83.7], [-0.01212, 75.1, 71.9], [0.012118, 87.2, 19.6], [-0.00541, 75, 29.9], [0.000164, 70.3, 75.8], [0.005518, 82.1, 86], [-0.00541, 74.2, 69.9], [-0.00027, 74.4, 60.8], [0.0842, 88.8, 62.3], [0.002518, 70.1, 91.4], [0.013107, 91.5, 85.1], [-0.00729, 81.8, 95.7], [-0.00355, 74.7, 80.6], [0.000107, 74.9, 66], [0, 72.1, 72.2], [0.000164, 69.7, 20.8], [0.000107, 71.4, 27.2], [-0.00477, 64.5, 32.5], [-0.00477, 65.4, 70.2], [0.004664, 70.2, 86], [0.003548, 80.7, 92.9], [-0.00355, 73.9, 78.5], [0.026359, 89.4, 68.7], [0, 92.4, 53.6], [0.061574, 84.9, 39.8], [0.011696, 76.6, 25.9], [-0.00027, 74.3, 57.9], [0.003631, 53.2, 12.2], [0, 59.8, 53.7], [0.004152, 70.6, 71.3], [0, 69.8, 64.7], [0.011262, 76.7, 53.4], [-0.00388, 66, 46.4], [0.020699, 86.3, 81], [0.011137, 90, 81], [-0.01114, 79.2, 75.2], [-0.00308, 84.5, 95.6], [-0.01084, 71.5, 86.4], [0.010836, 86.6, 94.4], [-0.01363, 73.5, 91.7], [0.002794, 69.6, 77.7], [0.005518, 76.5, 76.2], [0.004204, 54.4, 57], [0.003075, 60.3, 50.1], [-0.00427, 40.3, 54.9], [0.001219, 44.7, 84.3], [0.000891, 58.2, 94.5], [0, 56, 89.6], [0, 51.2, 88.7], [0.000738, 52.8, 65.4], [0, 57.8, 69.5], [0.002495, 61.5, 63.9], [0, 65, 69.3], [-0.00377, 65.2, 89.7], [-0.00185, 54.3, 64.1], [0, 51.6, 65.1], [0.001272, 50.8, 21.8], [0, 55, 33.5], [-0.00575, 33, 30.8], [-0.0006, 7.5, 51], [-0.00191, 7.1, 20.6], [0.000531, 2.9, 43.7], [0.001656, 45, 89.1], [-0.00125, 66.3, 89.3], [0, 65.4, 67.2], [0.016818, 85, 13.1], [-0.02636, 78.4, 36.8], [0.005681, 78.8, 78.2], [-0.00098, 79.8, 71.1], [0.006561, 75.7, 43], [0, 68.7, 46.4], [0.005681, 78.3, 19.7], [-0.01665, 71.7, 24.8], [0.003441, 80.7, 11.8], [-0.00355, 68.2, 20.2], [-0.00415, 65, 60.6], [0, 72.7, 57.2], [-0.00011, 67.9, 79.1], [0.000107, 71.6, 74.9], [-0.00011, 67.2, 78.6], [-0.0042, 37.3, 27.7], [-0.00054, 37.8, 14.3], [0.000545, 37.4, 36.1], [-0.00152, 32, 65.9], [0.000219, 29.8, 45.1], [-0.00022, 31.7, 63.6], [0.001303, 39.8, 40.7], [0.000823, 41, 20], [-0.00067, 29.7, 23.5], [0.003187, 44.3, 69.5], [0.00506, 64.5, 68.7], [0.002518, 74.1, 99.3], [0.00755, 80.7, 59.7], [-0.03587, 81, 62.6], [0.009374, 93.8, 5.3], [0.010363, 95.3, 6.5], [-0.01793, 82.2, 28.1], [0.000388, 20.7, 41.9], [-0.0019, 23.5, 24.7], [0.000814, 12.2, 51.8], [0.000277, 31.1, 66.2], [0.002144, 48.1, 72.3], [0.010836, 84.4, 94.1], [0.0045, 69.2, 56], [0.001303, 38.1, 45.4], [-0.0013, 25.8, 49.4], [-0.00062, 66.3, 25.6], [0, 57.4, 90.8], [0.003115, 61.3, 43.3], [-0.00066, 17.1, 43.1], [0.000662, 10.3, 56.7], [0.000497, 27.6, 59.3], [0.003187, 44.1, 72.2], [-0.00303, 39.6, 92.1], [-0.00149, 32.8, 90.9], [0.003473, 55, 96.2], [-0.00347, 39.5, 89.3], [0.000479, 33.9, 41], [0.002565, 52.4, 33.4], [0.001848, 62.5, 13.3], [0.000648, 62.2, 31], [0.004152, 67.1, 66.2], [0.00541, 81.6, 84.2], [0.0265, 95.9, 64.8], [0.001552, 37.7, 78.8], [0, 39, 87.2], [0.00853, 70.5, 96.2], [-0.00062, 9.6, 14.5], [0.001809, 26.4, 18.8], [0.000668, 40.1, 6.8], [3.93E-05, 55.3, 39.2], [0.004152, 68, 68.6], [0.00541, 77.5, 84.2], [0.000107, 72, 65.7], [0.00622, 61.8, 14.8], [-0.00065, 58.5, 13.9], [0.002518, 68.2, 7.9], [-0.00011, 72.2, 24.5], [0.016655, 85.7, 15.5], [-0.02636, 75.3, 29.6], [-0.00027, 72.4, 49.6], [0.000366, 29.9, 88.7], [0.009959, 72.8, 87.8], [-0.01212, 76.3, 67.8], [0.00443, 82.2, 27.4], [-3.93E-05, 57, 64.2], [-0.00201, 45.6, 46.3], [0.005125, 59.7, 47.6], [-0.00062, 63.7, 63.5], [0.004664, 73.9, 76.4], [-0.00098, 75.6, 27.7], [0.026359, 85, 32.2], [0.000948, 23.1, 90.8], [0.002915, 36, 31.7], [0.002044, 57, 10.4], [0.003473, 57.4, 96.5], [-0.00027, 67.8, 55.2], [0.005681, 75.2, 17], [-0.00088, 39.3, 9.5], [0.006188, 60.3, 4.7], [0.002901, 67.2, 26], [0.030789, 86.1, 74.3], [-0.0198, 76.8, 57.8], [-0.00252, 65.7, 91.5], [-0.00133, 58.3, 86.1], [0.000738, 55.8, 67.2], [0.002495, 58.9, 70.4], [0.003881, 71.7, 45.4], [-0.00263, 68.7, 97.5], [-1.33E-05, 6.4, 54.2], [0.001656, 46.7, 93.5], [-0.00377, 62.9, 8.2], [0.001267, 60, 61.5], [-0.00062, 65.4, 67.4], [0.001251, 71.4, 8.5], [0.000755, 21.1, 58.5], [-0.00075, 6, 52.4], [0.008524, 60.2, 66.2], [0, 66.4, 70.4], [0.004771, 67.7, 74.3], [0, 72.2, 62.9], [0, 72.2, 68.4], [0.040304, 98, 73.8], ] ) df2 = df.pivot_table("z", index="y", columns="x", aggfunc="sum").fillna(0) array = df2.sort_index(ascending=False).values
The beauty of pivot_table is that this will sort your x/y values.
Be carefull though, your y value would be ascending (top to bottom), which is the opposite of what you would want in a picture. You have to re-sort the index in reverse mode (hence the sort_index command).
There is also an assumption here : that your x’s and y’s values are linear OR that you are not trying to plot anything linear. In fact, you data sample are not (for instance, your first discrete y values are 3.5, 4, 4.1) ; you simply cannot use that kind of coordinates for anything in a grid format.
Given the fact that your datas’ coordinates are all float with one decimal (at most), you could complete your datas this way :
all_xs = all_ys = range(0, 1001) # get all discrete xs and ys values (on an integer basis) missing_xs = list(set(all_xs) - set(df['x']*10)) #find all missing xs missing_ys = list(set(all_ys) - set(df['y']*10)) #find all missing ys dummy_df = pd.DataFrame(0, index=missing_ys, columns=missing_xs) #construct a dummy dataframe with those xs and ys (filled with zeros) df2.index *= 10 #convert your indexes and columns of the previous df2 to integers df2.columns *= 10 df3 = df2.append(dummy_df).fillna(0) #append the dataframes, which will create the missing columns and index and fill all empty values with zeros df3.sort_index(inplace=True, ascending=False) #resort the index (ie ys values) df3.sort_index(axis=1, inplace=True, ascending=True) #resort the columns (ie xs values) print(df3.shape) #make sure the shape is a square array array = df3.values #extract the numpy array