pandas 无法从熊猫数据框中删除一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38288372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to drop a column from pandas dataframe
提问by Learnerbeaver
I have imported a Excel sheet into pandas. It has 7 columns which are numeric and 1 column which is a string (a flag).
我已将 Excel 工作表导入到 Pandas 中。它有 7 列数字和 1 列字符串(标志)。
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.
将标志转换为分类变量后,我试图从 Pandas 数据框中删除字符串列。但是,我无法做到。
Here's the code:
这是代码:
[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')
The column I'm trying to drop is 'Flag_median'.
我试图删除的列是“Flag_median”。
[In] parts_median_temp.drop('Flag_median')
[Out] ...ValueError: labels ['Flag_median'] not contained in axis
Help me drop the Flag_median
column from the Pandas dataframe.
帮我Flag_median
从 Pandas 数据框中删除该列。
回答by Chong Tang
You have to use the inplaceand axisparameter:
您必须使用inplace和axis参数:
parts_median_temp.drop('Flag_median', axis=1, inplace=True)
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.
'inplace' 的默认值为False,axis' 默认值为0。axis=0 表示按索引删除,而axis=1 将按列删除。
回答by Joe T. Boka
You can try this:
你可以试试这个:
parts_median_temp = parts_median_temp.drop('Flag_median', axis=1)