pythonstatisticspercentile

How to get the percentile of categories in a JSON file using Python?


How to count percentiles of categories located in an list in Python

I have this giant JSON to work with, assuming I'm new to Python (I do have background in programming), how can I get the percetile of each category included in the json?

I tried to at least get every category but I really don't think that the code I made is performatic.

def countCategory(file):
    categorieslist = list()
    for book in file:
        categories = book["categories"]
        for category in categories:
            if category not in categorieslist:
                categorieslist.append(category)
    return categorieslist
    

Solution

  • You should use "json.load(fp)" to read the json file, and use extend directly to append each item of the list to the list

    from collections import Counter
    
    def countCategory(file):
        categorieslist = list()
        with open(file) as fp:
            books = json.load(fp)
        for book in books:
            categories.extend(book["categories"])
        return categorieslist
    
    def get_category_percentage(categories):
        counter = Counter(categories)
        category_length = len(categories)
        for item, count in counter.items():
            percentage = (count / len(categories)) * 100
            print(f"{item}: {count} ({percentage:.2f}%)")