csv檔請參考前篇:
用 is np.na 做判斷式
本篇改用isna()重做
DataFrame.drop(labels=None, *, axis=0,
index=None, columns=None,
level=None, inplace=False, errors=’raise’)
fpath = r”C:\antenna_AMS\21046\emt2csv\01\01test.csv”
import pandas as pd
df_raw = pd.read_csv(fpath,header=None)
boolLst= list( pd.isna( df_raw[0] ) )
print(boolLst)
boolLst1 =pd.isna( df_raw[0] ).tolist()
print(boolLst1) #5,8列同效
#df_raw[0]只要一個[],若使用[[]],
#外觀一樣但型態會變成DataFrame
#tolist()會失敗,list()也會變成[0]
#df_raw[0]: 14列,第4,9列為NaN
nanIdx = []
for i in range(len(boolLst)):
if boolLst[i] == True: nanIdx.append(i)
print(“NaN index:”,nanIdx)
df_drop0 = df_raw.drop(nanIdx,axis=0)
#df_drop0 = df_raw.drop(nanIdx,axis=0).reset_index(drop=True)
#reset_index(drop=True) ,
#可以重置index,並將原index刪除
print(df_drop0)
輸出結果:
推薦hahow線上學習python: https://igrape.net/30afN
近期留言