无法从 pandas 数据框中删除一列 [英] Unable to drop a column from pandas dataframe

查看:81
本文介绍了无法从 pandas 数据框中删除一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 Excel 工作表导入到 Pandas 中.它有 7 列数字和 1 列字符串(标志).

I have imported a Excel sheet into pandas. It has 7 columns which are numeric and 1 column which is a string (a flag).

将标志转换为分类变量后,我试图从 Pandas 数据框中删除字符串列.但是,我做不到.

After converting the flag to a categorical variable, I am trying to drop the string column from the Pandas dataframe. However, I am not able to do it.

代码如下:

[In] parts_median_temp.columns

[Out] Index([u'PART_NBR', u'PRT_QTY', u'PRT_DOL', u'BTS_QTY', u'BTS_DOL', u'Median', u'Upper_Limit', u'Flag_median'], dtype='object')

我要删除的列是Flag_median".

The column I'm trying to drop is 'Flag_median'.

[In] parts_median_temp.drop('Flag_median') 

[Out] ...ValueError: labels ['Flag_median'] not contained in axis

帮我从 Pandas 数据框中删除 Flag_median 列.

Help me drop the Flag_median column from the Pandas dataframe.

推荐答案

你必须使用inplace 参数:

You have to use the inplace and axis parameter:

parts_median_temp.drop('Flag_median', axis=1, inplace=True)

'inplace' 的默认值为 False,axis' 的默认值为 0.axis=0 表示按索引删除,axis=1 表示按列删除.

The default value of 'inplace' is False, and axis' default is 0. axis=0 means dropping by index, whereas axis=1 will drop by column.

这篇关于无法从 pandas 数据框中删除一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆