【发布时间】:2018-01-09 21:00:35
【问题描述】:
以下警告被抛出,但我得到了实际的预期结果,当我尝试更改代码时它失败了。
new_table = new_table.convert_objects(convert_numeric=True)
new_table=new_table.replace(np.nan,0) # This is used to make - to 0 for calc
警告(来自警告模块): new_table = new_table.convert_objects(convert_numeric=True) FutureWarning:convert_objects 已弃用。使用特定于数据类型的转换器 pd.to_datetime、pd.to_timedelta 和 pd.to_numeric。
new_table 只不过是它包含的 pandas 数据框
A B C D E
1 - 3 5 6
2 3 5 6 7
- - 5 5 5
5 4 - - -
- - 4 - 4
9 - - 10 23
在这个给定的数据帧格式中,因为我们有字符串“-”,如果我使用下面的方法,进一步的求和或差异或乘法逻辑会抛出错误。
new_table = pd.to_numeric(new_table)
#new_table=new_table.replace("-",0)
new_table=new_table.replace(np.nan,0)
Traceback(最近一次调用最后一次): 文件第 107 行,在 new_table = pd.to_numeric(new_table) 文件第 113 行,在 to_numeric raise TypeError('arg 必须是列表、元组、一维数组或系列') TypeError: arg 必须是列表、元组、一维数组或系列
处理这种情况的最佳方法是什么,第一行应该是 str 格式的索引,其他行是数字,这样我的算术计算就不会受到影响。
有什么帮助吗?
【问题讨论】:
-
那么我的解决方案如何运作?
-
我会检查并反馈,谢谢!