Dropping a column from pandas dataframe - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Dropping a column from pandas dataframe (/thread-20872.html) |
Dropping a column from pandas dataframe - marco_ita - Sep-04-2019 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()
RE: Dropping a column from pandas dataframe - ThomasL - Sep-04-2019 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. RE: Dropping a column from pandas dataframe - perfringo - Sep-04-2019 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})
RE: Dropping a column from pandas dataframe - ThomasL - Sep-05-2019 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. RE: Dropping a column from pandas dataframe - perfringo - Sep-05-2019 If I observe data provided by OP then I think that last column (8th) label is (Vol., Price, Open, High, Low) RE: Dropping a column from pandas dataframe - ThomasL - Sep-05-2019 (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 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. RE: Dropping a column from pandas dataframe - marco_ita - Sep-07-2019 Thanks a lot for your help guys! |