0

I have a data frame consisting of 4 columns; date-time, wind speed, wind speed and wind direction. I need to bin the data in wind speed channels according to wind direction (12 sectors) and for every wind speed bin (1m/s, 2m/s, 3m/s and so on) and then calculate the mean of them. It would be easy if I needed to bin only according to wind direction or wind speed. I have found the answer for it:

binning data in python with scipy/numpy

However, I don't know how should I proceed binning according to two features.

I would appreciate it if someone has an idea.

Thank you very much in advance.

Wind_Eddies
  • 69
  • 2
  • 9

1 Answers1

0

Here is a recipe:

  • Convert your directions and speeds to bin indices if they are not already. You can use numpy.searchsorted for that.
  • Flatten the 2D bin indices using numpy.ravel_multi_index.
  • Use numpy.bincount on the flattened indices, once without weights and once with the quantity you want the mean of as weights. Then take the quotient to obtain the means. Optionally, cleanup any nans resulting from empty bins, (nan is technically the correct answer but may be undesirable nonetheless).
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99