簡體   English   中英

pandas dataframe 刪除列名 無

[英]pandas dataframe drop column name None

如題。 我有一個帶有None列的 dataframe,我想刪除它們。

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

列名不是字符串"None" ,它是None object。

我試圖像往常一樣drop

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)

以上所有,都不行。

刪除而不是獲取特定列的原因是因為列不一致,因此drop列是更好的選擇。

但是,如果有更好的方法或者我錯過了什么,請指導。 謝謝你。

我們可以使用del

...
del sheet_df[None]

由於None與 arguments 到DataFrame.drop的默認值一致,因此會出現混淆並且不會發生丟棄。

一種補救方法是提供一個包含 1 個元素的列表:

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

或等效地,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM