这是一个可能的解决方法
首先,您定义一个仅在需要时将数字转换为浮点数的函数
def to_number(s): try: s1 = float(s) return s1 except ValueError: return s
然后您逐行应用它。
例:
给定
df 0 0 a 1 2
其中
a和
2是字符串,我们通过以下方式进行转换
converted = df.apply(lambda f : to_number(f[0]) , axis = 1) converted 0 a 1 2
直接检查类型:
type(converted.iloc[0]) strtype(converted.iloc[1]) float



