- 背景:将物品的单价和数量相乘得到物品的总数,表达式如下
detail['payment'] = detail['counts']*detail['amounts']
,出现报错TypeError: can't multiply sequence by non-int of type 'str'
,查看这两类的数据类型用type(detail['payment'])
得到这两列都是string,所以我没需要转换数据类型来达到最终目的,转化为int或者float都可以,尝试了用表达式detail['payment'] = int(detail['counts'])*int(detail['amounts'])
,再次报错。 - 最终解决办法
detail['payment'] = detail['counts'].astype(int)*detail['amounts'].astype(int)
,成功解决此问题。
python--报错:TypeError: can‘t multiply sequence by non-int of type ‘str‘
于 2022-07-05 17:01:45 首次发布