简体   繁体   中英

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.

We can use del

...
del sheet_df[None]

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM