Python Forum
need help with binning data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help with binning data
#1
Hi there, I need help with binning some data for a homework question.
Say I have a list, [2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 19, 21], I need to sort them in bins increasing in 4's, starting with the lowest value.
The end format should be a dictionary, with how often the numbers appear inside each group:

Ideal output: {'2-6': 5, '6-10': 3, '10-14': 3, '14-18': 2, '18-22': 2}

I've written the following code but I just don't know how to count how often a number 'fits' into a bin, that is, adding the counted value to the key in the dictionary. I have managed to create a dictionary whose keys represent the bins, but I can't figure out how to sort the data in the list 'data' into the range determined by the bins.

That is, I can't figure out how to count the number of values in each bin, and then append that number to the values of the dictionary.

Can someone please help? Greatly appreciated :)

def bin_data():
    data = [2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 19, 21]
    data.sort()
    binsize = 4
    binned_data = {}

    def bin_data(data, binsize):
        numberofbins = int(round((max(data) - min(data)) / binsize)+0.5)
        for i in range(numberofbins):
            bin_lower = (i * binsize) + data[0]
            bin_upper = bin_lower + binsize
            binlower = int(bin_lower)
            binupper = int(bin_upper)
            binrange = (str(binlower) + '-' + str(binupper))

            binned_data.update({binrange: 1})

###Ideal Output: {'2-6': 5, '6-10': 3, '10-14': 3, '14-18': 2, '18-22': 2}
###x is count of frequency
Reply


Messages In This Thread
need help with binning data - by figure8 - Jul-21-2020, 11:17 AM
RE: need help with binning data - by DPaul - Jul-21-2020, 02:25 PM
RE: need help with binning data - by perfringo - Jul-21-2020, 02:51 PM
RE: need help with binning data - by deanhystad - Jul-21-2020, 03:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Data binning help please thatlakerboy 1 1,704 Jul-24-2020, 06:33 AM
Last Post: DPaul
  binning data stephd 2 4,155 Jan-22-2020, 04:44 PM
Last Post: geer26

Forum Jump:

User Panel Messages

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