首页 > 解决方案 > 修改 DataFrames 的切片:在 pandas 中使用什么来代替 .ix

问题描述

当我的应用程序崩溃时,我正在遍历 Pandas 中的 DataFrame。我必须从操作的最后一个索引开始对数据进行一些清理。我最终使用了这段代码:

df.ix[start_index:,["col"]] = ""

在我收到弃用警告后不久。但是,我花了很多时间寻找可以使用哪些功能来实现最终结果,.ix这是我唯一找到的东西。

将来,我将如何实现这样的目标?

标签: pythonpandas

解决方案


有 2 种可能的解决方案 - 使用DataFrame.ilocfor 替换位置,也使用按列名称位置替换Index.get_loc

df.iloc[start_index:,[df.columns.get_loc("col")]] = ""

或者您可以通过具有索引索引值的标签选择DataFrame.loc

df.loc[df.index[start_index]:,["col"]] = ""

推荐阅读