user6308605
user6308605

Reputation: 731

pandas dataframe drop column name None

As title. I have a dataframe with None column and I want to drop them.

sheet_df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 181 entries, 1 to 181
Data columns (total 4 columns):
 #   Column           Non-Null Count  Dtype  
---  ------           --------------  -----  
 0   Timestamp        181 non-null    object 
 1   Score            181 non-null    object 
 2   Full Name        181 non-null    object 
 3   None             181 non-null    object

The column name is not string "None", it is None object.

I attempted to drop as per usual :

sheet_df.drop(sheet_df[None], axis=1, inplace=True)
sheet_df.drop(None, axis=1, inplace=True)
sheet_df.drop(np.nan, axis=1, inplace=True)

All the above, do not work.

Reason of dropping instead of taking specific column is because the columns are inconsistent so drop the column is a better option.

However, if there is a better way of doing it or I missed something, please guide. Thank you.

Upvotes: 2

Views: 9224

Answers (2)

Prayson W. Daniel
Prayson W. Daniel

Reputation: 15606

We can use del

...
del sheet_df[None]

Upvotes: 2

Mustafa Aydın
Mustafa Aydın

Reputation: 18315

Since None coincides with the default values of the arguments to DataFrame.drop, confusion arises and no drop happens.

A remedy is to supply a list with 1 element:

df = df.drop([None], axis=1)

or equivalently,

df = df.drop(columns=[None])

Upvotes: 1

Related Questions