通过 pandas 对 csv 文件中列中字符串的出现求和

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

我有一个这种格式的 csv 文件:

file_name pred_class
First pound
Second sterling
Third pound
Fourth pound

通过 pandas 和 运行 加载文件后,此代码:

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

我收到这个错误

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

你能告诉我如何在没有这个错误的情况下正确地得到总和吗?

谢谢!

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

或使用:

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

虽然你的公式对我来说非常有效。

您可以尝试比较 numpy 数组,但您的解决方案对我来说工作得很好:

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