Unable to drop a column from pandas dataframe [duplicate]

人走茶凉 提交于 2021-02-16 10:25:30

问题


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

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.

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'.

[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.


回答1:


You have to use the inplace and axis parameter:

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.




回答2:


You can try this:

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


来源:https://stackoverflow.com/questions/38288372/unable-to-drop-a-column-from-pandas-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!