frequency of values in a field

Vlastimil Brom vlastimil.brom at gmail.com
Tue Feb 8 23:25:43 CET 2011


2011/2/8, Paul Rubin <no.email at nospam.invalid>:
> noydb <jenn.duerr at 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



More information about the Python-list mailing list