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
?
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
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
?
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