Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
binning data
#3
Corrected, now it works if the data sequence started with higher value than the bin size.

def make_bins(data, bin_size):
 
  binned_data = {}  #variable to store the output
  bin_low = 0  #initial lower limit of the bin
  bin_high = bin_size  #initial high limit of the bin
  key = str(bin_low) + '-' + str(bin_high)  #variable stores the key name
  i = 1  #counter for the values of the floats fits in the actal bin
   
  for data_piece in data:  #iter over the sorted data

    while data_piece < bin_low:
      bin_low += bin_size
      bin_high += bin_size
 
    if bin_low <= data_piece < bin_high:  #if the current data fits the bin, lower inclusive and upper exclusive
      binned_data[key] = i  #append the counter for the key
      i += 1  #increment the key
 
    else:  # if the current data doesn't fit the bin
      i = 1  #reset counter to 1
      bin_low += bin_size
      bin_high += bin_size  #and increment the bin limits by the bin size
      key = str(bin_low) + '-' + str(bin_high) # generate a new key
      binned_data[key] = i  #and append it 
 
  return(binned_data)
Reply


Messages In This Thread
binning data - by stephd - Jan-21-2020, 09:45 PM
RE: binning data - by geer26 - Jan-22-2020, 02:39 PM
binning data V2.0 - by geer26 - Jan-22-2020, 04:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Data binning help please thatlakerboy 1 2,462 Jul-24-2020, 06:33 AM
Last Post: DPaul
  need help with binning data figure8 3 3,091 Jul-21-2020, 03:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020