Python TypeError:/:';str';和';浮动';当试图通过Intererpolite时

Python TypeError:/:';str';和';浮动';当试图通过Intererpolite时,python,pandas,interpolation,Python,Pandas,Interpolation,代码: 我试图为cols(即float64)中的列插入缺少的数据,但是我不断得到以下错误: 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_i

代码:

我试图为
cols
(即
float64
)中的列插入缺少的数据,但是我不断得到以下错误:

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

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

您的
cols
列表似乎是字符串列表?它是如何数字化的?我正在使用cols索引我的数据帧。你是说这就是它断裂的原因?它们必须是数字索引吗?谢谢@sai对不起,我想念你。不太确定错误到底是什么。你能用你期望的数据类型来交叉检查
who.dtypes
吗?不用担心。我可以确认这些列实际上是float64,这意味着interpolate()方法应该可以工作
apply
返回更新的数据帧。尝试
df=who[cols]。应用(pd.to_numeric)
然后
df.interpolate(…)
who[cols] = who[cols].apply(lambda x: x.interpolate(method="nearest"), axis=1).astype(int)