import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 3),\
index=[“a”, “c”, “e”, “f”, “h”],\
columns=[“one”, “two”, “three”])
df2 = df.reindex([“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”])

df2[“one”]:

df2[“one”].isna():

df2[“one”].isna().tolist()
得到一個bool list
以下處理可以刪除空白列:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 3),\
index=[“a”, “c”, “e”, “f”, “h”],\
columns=[“one”, “two”, “three”])
df2 = df.reindex([“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”])
bool_list = df2[“one”].isna().tolist()
#[False, True, False, True, False, False, True, False]
nanIdx=[]
for i in range(len(bool_list)):
if bool_list[i] == True:nanIdx.append(i)
#nanIdx = [1, 3, 6]
df3 = df2.reset_index(drop=True)
#df2的index不是0,1,2,3…
df_drop = df3.drop(nanIdx,axis=0).reset_index(drop=True)
#對df2處理會出現
# KeyError: ‘[1, 3, 6] not found in axis’
#因為df2的index是a,b,c,d…
#如果df2的index沒有從0開始或沒連續,也要小心
#.reset_index(drop=True) 重要!

結果:


![Python: matplotlib繪圖,如何限定座標軸範圍? plt.axis([xmin, xmax, ymin, ymax]) Python: matplotlib繪圖,如何限定座標軸範圍? plt.axis([xmin, xmax, ymin, ymax])](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/02/20230208101745_93.jpg?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python:如何使用 PyMuPDF (import fitz ) 提取 PDF 文本區塊並存儲為 DataFrame ; text: List[ Tuple[float|str|int] ] = page.get_text(“blocks”) Python:如何使用 PyMuPDF (import fitz ) 提取 PDF 文本區塊並存儲為 DataFrame ; text: List[ Tuple[float|str|int] ] = page.get_text(“blocks”)](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2025/03/20250320084417_0_7783bd.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python如何讀取excel檔(.xlsx)?如何用欄標籤提取某一直行?df=pandas.read_excel() ; df[“欄標籤”] Python如何讀取excel檔(.xlsx)?如何用欄標籤提取某一直行?df=pandas.read_excel() ; df[“欄標籤”]](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2022/11/20221109163631_39.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![使用 Python 檢驗字符串格式:掌握正則表達式(Regular Expression)的起始^與終止$符號, pattern = r’^GATR[0-9]{4}$’ 使用 Python 檢驗字符串格式:掌握正則表達式(Regular Expression)的起始^與終止$符號, pattern = r’^GATR[0-9]{4}$’](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2024/07/20240712093637_0.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: 如何創建多層column name的pandas.DataFrame? df = pd.read_csv (‘data.csv’, header=[0, 1], sep=”,”) ; col = pd .MultiIndex .from_arrays( aryCol ) Python: 如何創建多層column name的pandas.DataFrame? df = pd.read_csv (‘data.csv’, header=[0, 1], sep=”,”) ; col = pd .MultiIndex .from_arrays( aryCol )](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230314164119_32.png?quality=90&zoom=2&ssl=1&resize=350%2C233)


近期留言