【问题标题】:Trouble with deleting a column in a dataframe based on the index of the column [duplicate]根据列的索引删除数据框中的列时遇到问题[重复]
【发布时间】:2018-12-04 12:19:36
【问题描述】:

似乎有一个逻辑错误。当我运行代码时,最后一列没有被删除


感谢任何帮助

A = [['Roy', 80, 75, 85, 90, 95, 0],
     ['John', 75, 80, 75, 85, 100, 0],
     ['Dave', 80, 80, 80, 90, 95, 0]]
A = pd.DataFrame(A)

这里我做一些操作来识别

 # Remove columns that meet a certain criteria.
 # This statement does not seem to be working because
 # at the end I get the same matrix without removing any column
A.drop(A.columns[i], axis=1)

【问题讨论】:

  • A=A.drop(A.columns[i], axis=1) 只需将其分配回去

标签: python python-3.x pandas dataframe


【解决方案1】:

正如@Wen 已经表明的那样:您需要重新分配结果。

A = A.drop(A.columns[i], axis=1)

你也可以在原地做而不重新分配:

A.drop(A.columns[i], axis=1, inplace=True)

【讨论】:

  • 使用A = A.loc[:, A.ne(0).any()]
  • 我使用的是 if np.all(A.iloc[:, j] == temp): A.drop(A.columns[j], axis=1, inplace=True)跨度>
【解决方案2】:

将其分配回A:

A = A.drop(A.columns[i], axis=1)

或使用就地选项:

A.drop(A.columns[i], axis=1, inplace =True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 2018-06-30
    • 1970-01-01
    • 2017-02-12
    相关资源
    最近更新 更多