Python Forum
Dropping a column from pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dropping a column from pandas dataframe
#1
Output:
Date Price Open High Low Vol. Change % (Vol., Price, Open, High, Low) 0 Sep 03, 2019 1,789.84 1,771.45 1,801.11 1,767.61 3.55 0.76% 3.55 1 Aug 30, 2019 1,776.29 1,797.49 1,799.74 1,764.57 3.06 -0.57% 3.06 2 Aug 29, 2019 1,786.40 1,783.00 1,798.55 1,777.25 3.02 1.26% 3.02 3 Aug 28, 2019 1,764.25 1,755.00 1,767.86 1,744.05 2.42 0.14% 2.42 4 Aug 27, 2019 1,761.83 1,775.73 1,779.40 1,746.68 3.03 -0.40% 3.03
I can't find the way to drop the last column (Vol., Price, Open, High, Low)
I tried with drop method but doesnt work.
Tried to add \ before ( and ) but doesnt work.
How can i get rid of the last column?
Thanks.


ERROR I GET when trying with the below code (with and without \ before ( and ):
df_amzn = df_amzn.drop(columns="(Vol., Price, Open, High, Low)")
df_amzn.head()
Error:
KeyError Traceback (most recent call last) <ipython-input-569-c204ff3c6958> in <module> ----> 1 df_amzn = df_amzn.drop(columns="(Vol., Price, Open, High, Low)") 2 df_amzn.head() /Applications/Anaconda/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in drop(self, labels, axis, index, columns, level, inplace, errors) 3938 index=index, columns=columns, 3939 level=level, inplace=inplace, -> 3940 errors=errors) 3941 3942 @rewrite_axis_style_signature('mapper', [('copy', True), /Applications/Anaconda/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in drop(self, labels, axis, index, columns, level, inplace, errors) 3778 for axis, labels in axes.items(): 3779 if labels is not None: -> 3780 obj = obj._drop_axis(labels, axis, level=level, errors=errors) 3781 3782 if inplace: /Applications/Anaconda/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in _drop_axis(self, labels, axis, level, errors) 3810 new_axis = axis.drop(labels, level=level, errors=errors) 3811 else: -> 3812 new_axis = axis.drop(labels, errors=errors) 3813 result = self.reindex(**{axis_name: new_axis}) 3814 /Applications/Anaconda/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in drop(self, labels, errors) 4963 if errors != 'ignore': 4964 raise KeyError( -> 4965 '{} not found in axis'.format(labels[mask])) 4966 indexer = indexer[~mask] 4967 return self.delete(indexer) KeyError: "['(Vol., Price, Open, High, Low)'] not found in axis"
Reply


Messages In This Thread
Dropping a column from pandas dataframe - by marco_ita - Sep-04-2019, 07:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  concat 3 columns of dataframe to one column flash77 2 840 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 1,026 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  attempt to split values from within a dataframe column mbrown009 8 2,349 Apr-10-2023, 02:06 AM
Last Post: mbrown009
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,117 Jan-06-2023, 10:53 PM
Last Post: haihal
  pandas column percentile nuncio 7 2,440 Aug-10-2022, 04:41 AM
Last Post: nuncio
  Pandas Dataframe Filtering based on rows mvdlm 0 1,430 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,311 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,260 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,794 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,549 Jan-10-2022, 04:42 PM
Last Post: moduki1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020