-2

I'd like to delete columns in a dataframe.
This is how I import the csv:

dffm = pd.read_csv('..xxxxxx.csv', sep=';', engine='python')

Why is it not possible to delete the column "High'?:

            Time        Open    High    Low     Close
Date                    
12.06.20    07:00:00    3046.50 3046.75 3046.00 3046.50
12.06.20    07:00:06    3046.75 3046.75 3046.00 3046.00
12.06.20    07:00:12    3046.00 3046.00 3045.75 3045.75
12.06.20    07:00:18    3046.00 3046.25 3046.00 3046.0

with this line:

dffm = dffm.drop(['High'], axis=1, inplace=True)

error:

"['High'] not found in axis"
Mark T
  • 145
  • 4
  • 12
  • 1
    Is it possible that there is some whitespaces before/after and or a line break in the column name? – Jonas Jul 11 '20 at 10:09
  • you can exclude `, inplace=True)` from your code. – David Erickson Jul 11 '20 at 10:09
  • 3
    Does this answer your question? [Python Pandas: drop a column from a multi-level column index?](https://stackoverflow.com/questions/25135578/python-pandas-drop-a-column-from-a-multi-level-column-index) – sushanth Jul 11 '20 at 10:11
  • @Jonas yes! You are right. `dffm.columns.str.strip()` solved the problem – Mark T Jul 11 '20 at 10:13

4 Answers4

1

hmm first of all the line you are using

dffm = dffm.drop(['High'], axis=1, inplace=True)

would have returned none if succeeded ,because inplace flag means it will do the operation on the current dataframe .

see: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html

try :

dffm.drop(columns=['High'], axis=1, inplace=True)

if that doesn't work you need to view your dataframe and see the column type, maybe it's not a string, that's a long shot but sometimes csvs string get change into byte string type. (you'll see a b"stringvalue") see : What is the difference between a string and a byte string?

Cptmaxon
  • 505
  • 2
  • 12
0

Possible cause of error is that column does not exists indeed, so check:

('High' in dffm.columns)

If result is False then seek for example for spaces in column names that make column name different.

ipj
  • 3,488
  • 1
  • 14
  • 18
0

Kindly try the following

# you can use columns parameter     
data = dffm.drop(columns="High") 

# when using inplace=True, you don't need to re-assign the dataframe , 
# as it directly modifies the datafame 
dffm.drop("High", axis=1, inplace=True).
0

You might be getting this error since you are using inplace=True and at the same time trying to save the returned DataFrame in dffm. However, doing it this way will is incorrect since when you turn on the inplace flag, the changes are done inplace and it returns None. You can read about it in the documentation of the drop operation of pandas.DataFrame https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html

You can do it using the general way of overwriting the dataframe with the one returned from the operation.

dffm = dffm.drop('High', axis=1)

Or you can use the inplace flag correctly and do it like,

dffm.drop('High', axis=1, inplace=True)
Sanket Mathur
  • 44
  • 2
  • 7