TypeError:尝试传递Intererpolite时,/:'str'和'float'的操作数类型不受支持

2024-09-23 16:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

cols = ["e_tbhiv_prct", "e_tbhiv_prct_lo", "e_tbhiv_prct_hi", "e_inc_tbhiv_100k", "e_inc_tbhiv_100k_lo",
       "e_inc_tbhiv_100k_hi", "e_inc_tbhiv_num", "e_inc_tbhiv_num_lo", "e_inc_tbhiv_num_hi"]

who[cols].apply(pd.to_numeric)

who[cols].interpolate(method="nearest", axis=1)

我试图为cols(它们是float64)中的列插入缺少的数据,但是我一直得到以下错误:

TypeError: unsupported operand type(s) for /: 'str' and 'float'

这是没有意义的,因为我的列是float格式的,并且是数字。我是否需要首先通过执行以下操作将这些列转换为 int

who[cols] = who[cols].apply(lambda x: x.interpolate(method="nearest"), axis=1).astype(int)

Tags: lohifloatnummethodincintapply