I was passing an Index type variable (Pandas.Index) containing the labels of columns I want to drop from my DataFrame and it was working correctly. It was Index type because I was extracting the column names based on certain condition from the DataFrame itself.
Afterwards, I needed to add another column name to that list, so I converted the Index object to a Python list so I could append the additional label name. But on passing the list as columns parameter to the drop() method on the Dataframe, I now keep getting the error :
ValueError: Need to specify at least one of 'labels', 'index' or 'columns'
How to resolve this error?
The code I use is like this:
unique_count = df.apply(pd.Series.nunique) redundant_columns = unique_count[unique_count == 1].index.values.tolist() redundant_columns.append('DESCRIPTION') print(redundant_columns) df.drop(columns=redundant_columns, inplace=True) Out: None I found why the error is occurring. After the append() statement, redundant_columns is becoming None. I don't know why. I would love if someone can explain why this is happening?
L = ['col1','col2']and thendrop(L)does not work? Can you show how usedrop?