#----------------------------------------------------- #範例16-15:df的查詢(2):條件where查詢2種方法:一般做法 vs df.query() #https://pandas.pydata.org/docs/user_guide/indexing.html #https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html #https://www.w3schools.com/python/pandas/ref_df_query.asp #https://www.geeksforgeeks.org/python-filtering-data-with-pandas-query-method/ #(1)檔案:學生成績檔(score6.xlsx),有不同班級 #https://acupun.site/lecture/python/py_example/chp16/score6.xlsx #(2)重點1:條件 where 查詢有2中做法: # (A) df[df['myclass']=='經管3a'] # (A) df.query('math >= 90') #(3)重點1:條件查詢 where 班級myclass = 經管3a的資料 # df[df['myclass']=='經管3a'] # 對應sql指令:select * from python where math >=90 #(4)重點2:而且查詢:國文英文數學,三者都高於70分的學生 # df[ (df['chi'] >=70) & (df['eng'] >=70) & (df['math'] >=70) ] #對應sql指令:select * from python where chi >=70 and eng >=70 and math >=70 #(5)重點3:或者查詢:英文學高於90,或是數學高於90 # df[ (df['eng'] >=90) | (df['math'] >=90) ] # 對應sql指令:select * from python where eng >=90 or math >=90 #(6)重點1:df.query()條件查詢 # df.query('math >= 90') # df.query('chi>=70 & eng>=70 & math>=70')) # df.query('chi>=70 and eng>=70 and math>=70')) # df.query('eng>=90 | math>=90')) # df.query('eng>=90 or math>=90')) #注意:query('math>90')的指令,與df['math']>90的寫法,不同 #------------------------------ #1.練習1:查詢myclass欄位 = "經管3a" #--------------------- #(1)讀取網路檔案:score6.xlsx(python資料表),有不同班級 #import pandas as pd #df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score5.xlsx","python") print() print("1-1.全部的資料=\n") #--------------------- #(2)查詢班級myclass=經管3a:df2 = df[['name','chi','eng','math']] #對應sql指令:select * from python where myclass='經管3a' print() #select1 = df['myclass'] == "經管3a" #df2 = df[select1] print("1-2.查詢班級myclass=經管3a=\n") print() print("1-2.查詢班級myclass=資工2a=\n") #------------------------------ #2.練習2:查詢math欄位大於90分 : df[df['math'] >= 90] #--------------------- #對應sql指令:select * from python where math >=90 print() print("2-1.查詢數學大於90分的學生=\n") #------------------------------ #2.練習3:查詢國文英文數學,三者都高於70分的學生 : df[(df['chi'] >=70) & (df['eng'] >=70) & (df['math'] >=70)] #--------------------- #對應sql指令:select * from python where chi >=70 and eng >=70 and math >=70 #注意:而且==> 在sql語法 ==> and #注意:而且==> 在pandas語法 ==> & print() print("2-2.查詢國文英文數學都高於70分的學生=\n") #------------------------------ #3.練習4:查詢英文學高於90,或是數學高於90的學生 : df[ (df['eng'] >=90) | (df['math'] >=90) ] #--------------------- #對應sql指令:select * from python where eng >=90 or math >=90 #注意:或 ==> 在sql語法 ==> or #注意:或 ==> 在pandas語法 ==> | print() print("2-3.查詢英文學高於90,或是數學高於90的學生=\n") #------------------------------ #4.練習5:query條件查詢 : df.query('math >= 90') #--------------------- #對應sql指令:select * from python where math >=90 print() print("3-1.query查詢數學大於90分的學生=\n") #注意:query('math>90')的指令,與df['math']>90的寫法,不同 print() print("3-2.query查詢國文英文數學都高於70分的學生=\n") print() print("3-3.query查詢英文學高於90,或是數學高於90的學生=\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-15:df的查詢(2):條件where查詢2種方法:一般做法 vs df.query() #https://pandas.pydata.org/docs/user_guide/indexing.html #https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html #https://www.w3schools.com/python/pandas/ref_df_query.asp #https://www.geeksforgeeks.org/python-filtering-data-with-pandas-query-method/ #(2)重點1:條件 where 查詢有2中做法: # (A) df[df['myclass']=='經管3a'] # (A) df.query('math >= 90') #(3)重點1:條件查詢 where 班級myclass = 經管3a的資料 # df[df['myclass']=='經管3a'] # 對應sql指令:select * from python where math >=90 #(4)重點2:而且查詢:國文英文數學,三者都高於70分的學生 # df[ (df['chi'] >=70) & (df['eng'] >=70) & (df['math'] >=70) ] #對應sql指令:select * from python where chi >=70 and eng >=70 and math >=70 #(5)重點3:或者查詢:英文學高於90,或是數學高於90 # df[ (df['eng'] >=90) | (df['math'] >=90) ] # 對應sql指令:select * from python where eng >=90 or math >=90 #(6)重點1:df.query()條件查詢 # df.query('math >= 90') # df.query('chi>=70 & eng>=70 & math>=70')) # df.query('chi>=70 and eng>=70 and math>=70')) # df.query('eng>=90 | math>=90')) # df.query('eng>=90 or math>=90')) #注意:query('math>90')的指令,與df['math']>90的寫法,不同 #------------------------------ #1.練習1:查詢myclass欄位 = "經管3a" #--------------------- #(1)讀取網路檔案:score6.xlsx(python資料表),有不同班級 import pandas as pd df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score5.xlsx","python") print() print("1-1.全部的資料=\n", df) #--------------------- #(2)查詢班級myclass=經管3a:df2 = df[['name','chi','eng','math']] #對應sql指令:select * from python where myclass='經管3a' print() select1 = df['myclass'] == "經管3a" df2 = df[select1] print("1-2.查詢班級myclass=經管3a=\n", df2) print() print("1-2.查詢班級myclass=資工2a=\n", df[df['myclass']=='資工2a']) #------------------------------ #2.練習2:查詢math欄位大於90分 : df[df['math'] >= 90] #--------------------- #對應sql指令:select * from python where math >=90 print() print("2-1.查詢數學大於90分的學生=\n", df[df['math'] >= 90]) #------------------------------ #2.練習3:查詢國文英文數學,三者都高於70分的學生 : df[(df['chi'] >=70) & (df['eng'] >=70) & (df['math'] >=70)] #--------------------- #對應sql指令:select * from python where chi >=70 and eng >=70 and math >=70 #注意:而且==> 在sql語法 ==> and #注意:而且==> 在pandas語法 ==> & print() select1 = (df['chi'] >=70) & (df['eng'] >=70) & (df['math'] >=70) print("2-2.查詢國文英文數學都高於70分的學生=\n", df[select1]) #------------------------------ #3.練習4:查詢英文學高於90,或是數學高於90的學生 : df[ (df['eng'] >=90) | (df['math'] >=90) ] #--------------------- #對應sql指令:select * from python where eng >=90 or math >=90 #注意:或 ==> 在sql語法 ==> or #注意:或 ==> 在pandas語法 ==> | print() select1 = (df['eng'] >=90) | (df['math'] >=90) print("2-3.查詢英文學高於90,或是數學高於90的學生=\n", df[select1]) #------------------------------ #4.練習5:query條件查詢 : df.query('math >= 90') #--------------------- #對應sql指令:select * from python where math >=90 print() print("3-1.query查詢數學大於90分的學生=\n", df.query('math >= 90')) #注意:query('math>90')的指令,與df['math']>90的寫法,不同 print() #print("3-2.query查詢國文英文數學都高於70分的學生=\n", df.query('chi>=70 & eng>=70 & math>=70')) print("3-2.query查詢國文英文數學都高於70分的學生=\n", df.query('chi>=70 and eng>=70 and math>=70')) print() #print("3-3.query查詢英文學高於90,或是數學高於90的學生=\n", df.query('eng>=90 | math>=90')) print("3-3.query查詢英文學高於90,或是數學高於90的學生=\n", df.query('eng>=90 or math>=90')) #-------------------- #(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 #--------------------------------------------------------------------