(Tutorial) Python Count – DataCamp

[ad_1]

When working with knowledge, you’re typically going to want to rely objects, create dictionaries values earlier than you recognize keys to retailer them in, or preserve order in a dictionary.

Counter is a robust instrument for counting, validating, and studying extra in regards to the components inside a dataset that’s discovered within the collections module. You move an iterable (checklist, set, tuple) or a dictionary to the Counter. You can too use the Counter object equally to a dictionary with key/worth task, for instance, counter[key] = worth.

A standard utilization for Counter is checking knowledge for consistency previous to utilizing it.

Counter Module

The counter module relies on dictionary; you need to use the entire regular dictionary options. On this instance, we have now the checklist named nyc_eatery_types that accommodates one column of information referred to as kind from a desk about eateries in NYC parks. We create a brand new counter based mostly on that checklist and print it.

from collections import Counter
nyc_eatery_count_by_types = Counter(nyc_eatery_types)
print(nyc_eatery_count_by_types)
Counter({'Cell Meals Truck': 114, 'Meals Cart': 74, 'Snack Bar': 24,
'Specialty Cart': 18, 'Restaurant': 15, 'Fruit & Vegetable Cart': 4})

You may see every kind from the checklist and the variety of occasions it was discovered within the checklist. We will additionally see what number of eating places are within the counter through the use of Restaurant because the index and printing it.

print(nyc_eatery_count_by_types['Restaurant'])
15

Utilizing Counter to Discover the Most Widespread

Counters additionally present an exquisite option to discover the commonest values they comprise. The most_common() methodology on a Counter returns a listing of tuples containing the objects and their rely in descending order.

Let’s print the highest Three eatery varieties within the NYC park system with the most_common() methodology and move it Three because the quantity objects to return.

print(nyc_eatery_count_by_types.most_common(3))
[('Mobile Food Truck': 114), ('Food Cart': 74), ('Snack Bar': 24)]

most_common() is superb for frequency analytics and discovering out how typically an merchandise happens.

Interactive Instance

Within the following instance, you’ll:

  • Use the information from the Chicago Transit Authority on ridership.
  • Import the Counter object from collections.
  • Print the primary ten objects from the stations checklist.
  • Create a Counter of the stations checklist referred to as station_count.
  • Print the station_count.
# Import the Counter object
from collections import Counter

# Print the primary ten objects from the stations checklist
print(stations[:10])

# Create a Counter of the stations checklist: station_count
station_count = Counter(stations)

# Print the station_count
print(station_count)

After we run the above code, it produces the next outcome:

['stationname', 'Austin-Forest Park', 'Austin-Forest Park', 'Austin-Forest Park', 'Austin-Forest Park', 'Austin-Forest Park', 'Austin-Forest Park', 'Austin-Forest Park', 'Austin-Forest Park', 'Austin-Forest Park']
Counter({'California-Cermak': 700, 'Damen-Cermak': 700, 'Ashland-Orange': 700, 'Argyle': 700, 'Halsted-Orange': 700, 'Laramie': 700, 'Diversey': 700, '79th': 700, 'Clinton-Lake': 700, 'Monroe/Dearborn': 700, 'Wellington': 700, 'Merchandise Mart': 700, 'Cicero-Cermak': 700, 'Kedzie-Lake': 700, 'Southport': 700, 'Washington/Wells': 700, 'Clark/Division': 700, 'stationname': 1})

Try it for yourself.

To study extra in regards to the collections module for counting, please see this video from our course Data Types for Data Science in Python.

This content material is taken from DataCamp’s Data Types for Data Science in Python course by Jason Myers.

[ad_2]

Source link

Write a comment