我有一个包含许多(但不是全部)十进制128列的数据帧(取自mongodb集合)。我无法对它们进行任何数学或比较(例如,“Decimal128”和“float”实例之间不支持“<;”)。在
什么是最快/最简单的方法来转换所有这些浮动或更简单的内置类型,我可以使用?在
有Decimal128 to_decimal()方法和pandas astype(),但如何在一个step/helper方法中对所有(Decimal128)列执行此操作?在
编辑,我试过:
testdf = my_df.apply(lambda x: x.astype(str).astype(float) if isinstance(x, Decimal128) else x)
testdf[testdf["MyCol"] > 80].head()
但我得到:
^{pr2}$使用.astype(str).astype(float)转换单个列。在
正在转换完整的数据帧。在
对于单列。IDs是列的名称。在
^{pr2}$测试实施
输出:
只需使用:
{{a1}和前面的}方法相比,你也可以使用^或}方法。在
^{pr2}$我无法在我的系统中重现十进制128。你能看看下一行对你行吗?在
它将检查列是否为Decimal128类型,然后将其转换为float。在
相关问题 更多 >
编程相关推荐