我的问题与此非常相似,但我需要转换整个数据帧而不仅仅是一系列.该to_numeric函数一次只能在一个系列上运行,并且不能替代已弃用的convert_objects命令.有没有办法convert_objects(convert_numeric=True)在新的pandas版本中获得与命令类似的结果?
谢谢MikeMüller的例子.df.apply(pd.to_numeric)如果值都可以转换为整数,则效果很好.如果在我的数据框中我有无法转换为整数的字符串怎么办?例:
df = pd.DataFrame({'ints': ['3', '5'], 'Words': ['Kobe', 'Bryant']})
df.dtypes
Out[59]:
Words object
ints object
dtype: object
Run Code Online (Sandbox Code Playgroud)
然后我可以运行已弃用的函数并获取:
df = df.convert_objects(convert_numeric=True)
df.dtypes
Out[60]:
Words object
ints int64
dtype: object
Run Code Online (Sandbox Code Playgroud)
运行该apply命令会给我带来错误,即使是尝试和处理也是如此.