Sum the occurance of string in column in csv file via pandas

Viewed 295

I have a csv file in this format:

file_name pred_class
First pound
Second sterling
Third pound
Fourth pound

After loading the file via pandas, and running this code:

total = (df['pred_class'] == 'pound').sum()
print(total)

I get this error

raise UnsupportedArrayTypeException(type_name) console_thrift.UnsupportedArrayTypeException: UnsupportedArrayTypeException(type='int64')

Can you tell me how to correctly get the sum without this error?

Thanks!

2 Answers
len(df[df['pred_class'] == 'pound'])

or use:

sum(i == True for i in df['pred_class'] == 'pound')

Your formula is working perfectly for me though.

You can try compare numpy array, but your solution for me working perfectly:

total = (df['pred_class'].to_numpy() == 'pound').sum()
Related