How to calculate totals from Pandas dataframe

Question:

I am working on a method for calculating the weights from a given dataset. The method that I’m using is written like so:

def wmc(classAttribute,attribute,training_set):
    attributeCount = training_set[training_set[classAttribute] == attribute].count()[classAttribute]
    total          = training_set[classAttribute].count()[classAttribute]
    print(f'{attributeCount}/{total}')
    return attributeCount/total

The expected values for attributeCount and total are supposed to be the total number of records with equivalent to attribute and the total number of records for the given dataset.

However, these comeback as non-numeric types. How can I get the count of records from the dataset where value == attribute?

Asked By: Evan Gertis

||

Answers:

This will do it.

def wmc(classAttribute,attribute,training_set):
    attributeCount = len(training_set[training_set[classAttribute] == attribute].index)
    total          = len(training_set[classAttribute].index)
    print(f'{attributeCount}/{total}')
    return attributeCount/total
    
Answered By: Evan Gertis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.