在帧上迭代以删除列

2024-03-29 00:11:59 发布

您现在位置:Python中文网/ 问答频道 /正文

以下for循环在两个数据帧上迭代不起作用:

for frame in [df_train, df_test]:
    frame = frame.drop('Embarked', axis=1)

我没有收到错误消息,但两个数据帧中没有删除“已装载”列。为什么?你知道吗


Tags: 数据intest消息dffor错误train
1条回答
网友
1楼 · 发布于 2024-03-29 00:11:59

help(frame.drop)

def drop(self, labels, axis=0, level=None, inplace=False, **kwargs):
    """
    Return new object with labels in requested axis removed

    ...

    inplace : bool, default False
        If True, do operation inplace and return None.

现在,您只需创建新对象并将它们命名为frame,这不会影响列表中的任何内容。您可以使用inplace=True来影响原始对象:

for frame in [df_train, df_test]:
    frame.drop('Embarked', axis=1, inplace=True)

相关问题 更多 >