删除熊猫框架中的列

I have been trying the past 40 minutes to delete a specific column (Name) from my data frame but no method seems to work. I am using jupyter notebook by the way, not sure if that may be an issue. I am also 100% sure that the column name is named Name and that it is not the index of the data frame -- I am using the Titanic data set.

我尝试过的

  1. del df['Name']
  2. df.drop('Name', axis=1, inplace=True)
  3. df = df.drop('Name', 1)

以上所有内容似乎都删除了该列,但是当重新读取csv时,该列又出现了。关于为什么发生这种情况的任何线索?

评论
  • in_id
    in_id 回复

    With reading a csv file and loading its content in a dataframe, you create a python object called df.

    Then, with the three methods you showed us, you only delete some content of this object, not the csv's content.

    I would recommend you to use the first method, del, keep working with the dataframe and not reading the csv again.

    请让我知道是否有任何不清楚的地方。

  • 给爷爬远点
    给爷爬远点 回复

    当您将csv读取到Pandas DataFrame时,会将数据加载到内存中。对数据帧做任何事情都不会改变您的csv。我建议您将修改后的数据框导出为新的CSV

    df.to_csv(r'Path where you want to store the exported CSV file\File Name.csv', index = False)

    DOCS