Bottom Page

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"
scidam wrote Sep-05-2019, 12:12 AM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#2
What about looking at the documentation to pandas.DtaFrame.drop()?
Especially look at the examples and then you should realize that this line
df_amzn = df_amzn.drop(columns="(Vol., Price, Open, High, Low)")
doesn´t work because the parameter columns= is faulty.
Quote
#3
You should check the documentation of pandas.DataFrame.drop and version of pandas you are using (for example 'columns' is available from pandas 0.21)

It works for me with my dummy dataframe both with labels and with columns:

- df.drop(labels='(Vol., Price, Open, High, Low)', axis=1)
- df.drop(columns='(Vol., Price, Open, High, Low)')

If you want change the dataframe then you should set inplace:

- df.drop(columns='(Vol., Price, Open, High, Low)', inplace=True)

EDIT:

ninjad by ThomasL

I used this code to create dummy dataframe:

df = pd.DataFrame({'numbers': range(1, 4), 'letters': [*'abc'], '(Vol., Price, Open, High, Low)': 42})
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#4
this line
df_amzn = df_amzn.drop(columns="(Vol., Price, Open, High, Low)")
has to be
df_amzn = df_amzn.drop(columns=["Vol.", "Price", "Open", "High", "Low"])
then pd.drop() works well.
Quote
#5
If I observe data provided by OP then I think that last column (8th) label is (Vol., Price, Open, High, Low)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#6
(Sep-05-2019, 01:17 PM)perfringo Wrote: If I observe data provided by OP then I think that last column (8th) label is (Vol., Price, Open, High, Low)

Definitely reading a post thoroughly makes the difference, of course, you´re right.

And OPs problem
Error:
KeyError: "['(Vol., Price, Open, High, Low)'] not found in axis"
results from
df_amzn = df_amzn.drop(columns="(Vol., Price, Open, High, Low)")
missing the axis="columns" parameter (or axis=1)
as axis="index" is the default and "(Vol., Price, Open, High, Low)" is not in the index.
df_amzn = df_amzn.drop(columns="(Vol., Price, Open, High, Low)", axis="columns")
as you already mentioned in your examples.
Quote
#7
Thanks a lot for your help guys!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Obtaining Correct Date In Pandas DataFrame eddywinch82 5 309 Jan-17-2020, 05:15 PM
Last Post: eddywinch82
  Parse XML String in Pandas Dataframe creedX 2 273 Dec-09-2019, 07:35 PM
Last Post: creedX
  itertuples, new column, datetime, pandas karlito 6 332 Nov-29-2019, 11:07 AM
Last Post: karlito
  Pandas dataframe to join three tables using like condition among them sandeep_ganga 0 237 Nov-29-2019, 08:30 AM
Last Post: sandeep_ganga
  Pandas Dataframe to Google Big Query Ecniv 2 879 Nov-21-2019, 02:26 PM
Last Post: Ecniv
  manipulating a dataframe - pandas nsx200 2 215 Nov-14-2019, 10:38 AM
Last Post: nsx200
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 214 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  pandas dataframe iloc mystery edvvardbrian 2 297 Oct-29-2019, 02:55 PM
Last Post: jefsummers
  sort values of a column pandas karlito 2 354 Oct-22-2019, 06:11 AM
Last Post: karlito
  How to add a few empty rows into a pandas dataframe python_newbie09 2 1,217 Sep-20-2019, 08:52 AM
Last Post: python_newbie09

Forum Jump:


Users browsing this thread: 1 Guest(s)