1

I have a DataFrame in the form of:

   Lon Lat  N
0   2   1   1
1   2   2   2
2   2   3   1
3   2   2   1
4   3   2   1
5   3   2   1

where Lon and Lat represent the coordinates (locations), N represents the count number at corresponding locations.

Now, I want to:

  1. Compute the sum of N at each point to achieve as follows:
   Lon Lat  N
0   2   1   1
1   2   2   3
2   2   3   1
3   3   2   2
  1. Given a meshgrid Lon_grid = np.arange(1,5), Lat_grid = np.arange(1,4), i.e., a 4 * 3 matrix, and then plot the data in contour. Note that locations with no data can be filled with NaN or 0.

If there is not enough data to make a contour, the final results can be like this:

   Lon Lat  N
0   1   1   0
1   2   1   1
2   3   1   0
3   4   1   0
4   1   2   0
5   2   2   3
6   3   2   2
7   4   2   0
8   1   3   0
9   2   3   1
10  3   3   0
11  4   3   0

Thank you very much for your help!

Feng Hu
  • 63
  • 6
  • You probably don"t have enough data for a contour plot – mozway Oct 17 '22 at 17:05
  • Thank you for your comment. This is just a simplified example. Maybe the amount of data is limited. All I want to know is how to groupby the same coordinates to sum the counts and then fill the mesh grid with 0s. – Feng Hu Oct 17 '22 at 17:17
  • I edited the question to give the preferred final result. Hope it helps. Thanks. – Feng Hu Oct 17 '22 at 17:24

1 Answers1

1

Here is an example with the provided data:

df2 = df.pivot_table(index='Lon', columns='Lat', values='N',
                     aggfunc='sum', fill_value=0)


X,Y = np.meshgrid(df2.index.to_numpy(), df2.columns.to_numpy())

plt.contourf(X, Y, df2.values.T)
plt.colorbar()
plt.show()

output:

contour plot

For the aggregated data (which I didn't use here):

out = df.groupby(['Lon', 'Lat'], as_index=False).sum()
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Thanks for your answer! But I still have one question, that is how to get the final table starting from `out`? Meaning adding lines containing 0s to the `out`. Can I do it without a loop? – Feng Hu Oct 18 '22 at 04:27
  • @FengHu you would need to [`pivot`](https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe) – mozway Oct 18 '22 at 05:09