#----------------------------------------------------- #範例16-17:查詢缺值,NAN的資料:isnull(),isna(),notna() #https://datatofish.com/rows-with-nan-pandas-dataframe/ #https://www.w3schools.com/python/pandas/ref_df_isnull.asp #https://pandas.pydata.org/docs/reference/api/pandas.isnull.html #(1)檔案:學生成績檔(score1.xlsx),有缺值 #https://acupun.site/lecture/python/py_example/chp16/score1.xlsx #(2)重點1:檢查某個欄位,找出有缺值NAN的 # 在SQL語法:select * from mad3a where math IS NULL # 在pandas:df[df['欄位名稱'].isna()] # 在pandas:df[df['欄位名稱'].isnull()] # df[ 判斷式 ] #(3)重點2:檢查某個欄位,找出沒有缺值NAN的 # 在SQL語法:select * from mad3a where math IS NOT NULL # 在pandas:df[df['欄位名稱'].notna()] #(4)重點3:檢查全部欄位,找出有缺值NAN的 # 在pandas:df[ df.isna().any(axis=1) ] # 在pandas:df[ df.isnull().any(axis=1) ] # df[ 判斷式 ] # axis = 0 refers to : horizontal axis or rows # axis = 1 refers to : vertical axis or columns. #------------------------------ #1.練習1:查詢某個欄位的缺值資料: #------------------------------ #(1)讀取網路檔案:(score1.xlsx),有缺值 #import pandas as pd #df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score1.xlsx","mad3a") print() print("1-1.全部的資料=\n") #--------------------- #(2)查詢數學有缺值資料: df[ df['chi'].isnull() ] 或是 df[ df['chi'].isna() ] #對應sql指令:select * from mad3a where math IS NULL print() print("1-2.查詢數學有缺值資料=\n") print() print("1-3.查詢國文有缺值資料=\n") print() #pandas判斷有缺值的函數,有2個:isna(), isnull() print("1-4.查詢英文有缺值資料=\n") #------------------------------ #2.練習2:查詢全部欄位的缺值資料: #------------------------------ #(1)查詢全部欄位有缺值資料:df[df.isna().any(axis=1)], 或是 df[df.isnull().any(axis=1)] #對應sql指令:select * from mad3a where chi IS NULL or eng IS NULL or math IS NULL print() #注意: 不可以用大寫Axis print("2-1.查詢全部欄位有缺值資料=\n") print() print("2-2.查詢全部欄位有缺值資料=\n") #------------------------------ #3.練習3:查詢某個欄位沒有缺值的資料: #------------------------------ #對應sql指令:select * from mad3a where math IS NOT NULL print() print("3-1.查詢數學沒有缺值資料=\n") print() print("3-2.查詢英文沒有缺值資料=\n") #-------------------- #(5)把df,儲存為score1c.xlsx print() #(A).儲存到虛擬專案區(暫存幾天) #df.to_excel("score5.xlsx") #(A).加入google雲端的目錄(永久保存) #A.先在左邊的『檔案』, #B.點按『掛接雲端硬碟』,顯示『drive』 #import os #os.chdir("/content/drive/MyDrive/Colab Notebooks") #df.to_excel("score1c.xlsx") #查看目錄的檔案名稱 !ls #-------------------------------------------------------------------- #-------------------------------------------------------------------- #範例16-17:查詢缺值,NAN的資料:isnull(),isna(),notna() #https://datatofish.com/rows-with-nan-pandas-dataframe/ #https://www.w3schools.com/python/pandas/ref_df_isnull.asp #https://pandas.pydata.org/docs/reference/api/pandas.isnull.html #(1)檔案:學生成績檔(score1.xlsx),有缺值 #https://acupun.site/lecture/python/py_example/chp16/score1.xlsx #(2)重點1:檢查某個欄位,找出有缺值NAN的 # 在SQL語法:select * from mad3a where math IS NULL # 在pandas:df[df['欄位名稱'].isna()] # 在pandas:df[df['欄位名稱'].isnull()] # df[ 判斷式 ] #(3)重點2:檢查某個欄位,找出沒有缺值NAN的 # 在SQL語法:select * from mad3a where math IS NOT NULL # 在pandas:df[df['欄位名稱'].notna()] #(4)重點3:檢查全部欄位,找出有缺值NAN的 # 在pandas:df[ df.isna().any(axis=1) ] # 在pandas:df[ df.isnull().any(axis=1) ] # df[ 判斷式 ] # axis = 0 refers to : horizontal axis or rows # axis = 1 refers to : vertical axis or columns. #------------------------------ #1.練習1:查詢某個欄位的缺值資料: #------------------------------ #(1)讀取網路檔案:(score1.xlsx),有缺值 import pandas as pd df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score1.xlsx","mad3a") print() print("1-1.全部的資料=\n", df) #--------------------- #(2)查詢數學有缺值資料: df[ df['chi'].isnull() ] 或是 df[ df['chi'].isna() ] #對應sql指令:select * from mad3a where math IS NULL print() select1 = df['math'].isnull() print("1-2.查詢數學有缺值資料=\n", df[select1] ) print() print("1-3.查詢國文有缺值資料=\n", df[ df['chi'].isnull() ] ) print() #pandas判斷有缺值的函數,有2個:isna(), isnull() print("1-4.查詢英文有缺值資料=\n", df[ df['eng'].isna() ] ) #------------------------------ #2.練習2:查詢全部欄位的缺值資料: #------------------------------ #(1)查詢全部欄位有缺值資料:df[df.isna().any(axis=1)], 或是 df[df.isnull().any(axis=1)] #對應sql指令:select * from mad3a where chi IS NULL or eng IS NULL or math IS NULL print() select1 = df.isnull().any(axis=1) #注意: 不可以用大寫Axis print("2-1.查詢全部欄位有缺值資料=\n", df[ select1 ] ) print() print("2-2.查詢全部欄位有缺值資料=\n", df[df.isna().any(axis=1)] ) #------------------------------ #3.練習3:查詢某個欄位沒有缺值的資料: #------------------------------ #對應sql指令:select * from mad3a where math IS NOT NULL print() select1 = df['math'].notna() print("3-1.查詢數學沒有缺值資料=\n", df[select1] ) print() print("3-2.查詢英文沒有缺值資料=\n", df[ df['eng'].notna() ] ) #-------------------- #(5)把df,儲存為score1c.xlsx print() #(A).儲存到虛擬專案區(暫存幾天) #df.to_excel("score5.xlsx") #(A).加入google雲端的目錄(永久保存) #A.先在左邊的『檔案』, #B.點按『掛接雲端硬碟』,顯示『drive』 #import os #os.chdir("/content/drive/MyDrive/Colab Notebooks") #df.to_excel("score1c.xlsx") #查看目錄的檔案名稱 !ls #--------------------------------------------------------------------