Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55727 > unrolled thread

Re: frequency of values in a field

Started byVlastimil Brom <vlastimil.brom@gmail.com>
First post2011-02-08 23:25 +0100
Last post2011-02-08 23:25 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: frequency of values in a field Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-02-08 23:25 +0100

#55727 — Re: frequency of values in a field

FromVlastimil Brom <vlastimil.brom@gmail.com>
Date2011-02-08 23:25 +0100
SubjectRe: frequency of values in a field
Message-ID<mailman.21.1297203945.1633.python-list@python.org>
2011/2/8, Paul Rubin <no.email@nospam.invalid>:
> noydb <jenn.duerr@gmail.com> writes:
>> I am looking for ways to go about capturing the frequency of unique
>> values in one field in a dbf table which contains ~50k records.  The
>> values are numbers with atleast 5 digits to the right of the decimal,
>> but I want the frequency of values to only 2 decimal places.  I do
>> have a method to do this courtesy of a provided tool in ArcGIS.  Was
>> just curious about ways to do it without arcgis sw, using just python.
>...
>
>     from decimal import Decimal as D
>     from collections import defaultdict
>
>     records = ['3.14159','2.71828','3.142857']
>
>     td = defaultdict(int)
>     for x in records:
>         td[D(x).quantize(D('0.01'))] += 1
>
>     print td
>
>>...

Another variant of the above code using collections.Counter (in newer
python versions);
The actual frequency counting code is actually the single
instantiation of the Counter  from an iterable. The appropriate
handling of the number values might be tweaked as needed.

>>> from decimal import Decimal as D
>>> from collections import Counter
>>> records = ['3.14159','2.71828','3.142857']
>>> Counter(D(x).quantize(D('0.01')) for x in records)
Counter({Decimal('3.14'): 2, Decimal('2.72'): 1})
>>>

vbr

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web