Search code examples
pythonpandasmatrixgridgeo

Converting Pandas table into matrix to grid


I have a pandas data set of Longitude height and averaged carbon monoxide data points. I would like to plot these on a grid of height vs longitude with color mapped CO values. My values are in the formate:

longitude height   CO
71        8000.0   50.958159
          9000.0   59.076651
          10000.0  46.716544
          11000.0  43.170888
72        8000.0   45.724138
          9000.0   45.505567
          10000.0  40.749734
          11000.0  42.305107
73        8000.0   53.045872
          9000.0   56.013487
          10000.0  42.418022
          11000.0  40.897789
74        7000.0   48.440000
          8000.0   59.165261
          9000.0   50.215405
          10000.0  42.504561
          11000.0  46.189446
75        7000.0   47.590909
          8000.0   38.887422
          9000.0   33.653982
          10000.0  47.762696
          11000.0  45.612828

I have attempted to do this by occupying a matrix of all the relevant values:

matrix = np.zeros(shape=(30, 12))

for i in range(70,100):
    for j in range(0,12):
        h = j*1000
        if grid_data.loc[(grid_data['longitude']) & (grid_data.loc(grid_data['height'] == h))
            x = i-70
            val = (grid_data.loc[(grid_data['longitude'] = i) & (grid_data['height'] = h))
            matrix[x,j] = val['CO']

However, I realize this is just blatently wrong as I am doing if statements on data frames. I have no idea how to move on from here so any help would be greatly appreciated.


Solution

  • Suppose your dataframe named 'df', you can use df.pivot to 'map' it, just like:

    df.pivot(index="longitude", columns="height", values="CO")