Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #55727
| References | <2f463773-da89-4c50-bd4f-8f891e6038a7@d12g2000vbz.googlegroups.com> <7x7hdarzol.fsf@ruckus.brouhaha.com> |
|---|---|
| Date | 2011-02-08 23:25 +0100 |
| Subject | Re: frequency of values in a field |
| From | Vlastimil Brom <vlastimil.brom@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.21.1297203945.1633.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: frequency of values in a field Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-02-08 23:25 +0100
csiph-web