#範例16-9:排序:sort_values #https://www.w3schools.com/python/pandas/ref_df_sort_values.asp #https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html #(1)檔案:學生成績檔(excel) #https://acupun.site/lecture/python/py_example/chp16/score2.xlsx #多科同分檔案 #https://acupun.site/lecture/python/py_example/chp16/score3.xlsx #有缺值(NaN)檔案 #https://acupun.site/lecture/python/py_example/chp16/score1.xlsx #(1)重點: #正向排序:df.sort_values(by="math") #逆向排序:df.sort_values(by="math", ascending=False) #數學同分則比英文,英文同分則比國文: df.sort_values(by=["math","eng","chi"]) #將缺值(NaN)會放到最上面:df.sort_values(by="math", na_position='first') #------------------------------ #1.練習1:分析學生成績檔(excel) #--------------------- #(1)讀取網路檔案 #--------------------- #(2)以數學分數正向排序:df.sort_values(by="math") print() print("2. 還沒有排序前的序位=\n") print() print("以數學分數正向排序=\n") #--------------------- #(3)以數學分數逆向排序:df.sort_values(by="math", ascending=False) print() print("3. 以數學分數逆向排序=\n") print() #--------------------- #(4)讀取網路檔案:有同分情況下的排序(score3.xlsx) #import pandas as pd #df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score3.xlsx","mad3a") print() print("4. 還沒有排序前的序位=\n") print() print("以數學分數正向排序,3位數學同分=\n") #--------------------- #(5)數學同分則比英文,英文同分則比國文: df.sort_values(by=["math","eng","chi"]) print() print("5. 數學同分則比英文,英文同分則比國文=\n") print() print("由高排到低分,數學同分則比英文,英文同分則比國文=\n") #--------------------- #(6)讀取網路檔案:有缺值(NaN)下的排序(score1.xlsx) #import pandas as pd #df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score1.xlsx","mad3a") print() print("6. 還沒有排序前的序位=\n") print() print("以數學分數正向排序(預設缺值(NaN)會放到最後)=\n") #--------------------- #(7)將缺值(NaN)會放到最上面:na_position='first' print() print("7. 以數學分數正向排序(將缺值(NaN)會放到最上面)=\n") #----------------------------------------------------- #----------------------------------------------------- #範例16-9:排序:sort_values #https://www.w3schools.com/python/pandas/ref_df_sort_values.asp #https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html #(1)檔案:學生成績檔(excel) #https://acupun.site/lecture/python/py_example/chp16/score2.xlsx #多科同分檔案 #https://acupun.site/lecture/python/py_example/chp16/score3.xlsx #有缺值(NaN)檔案 #https://acupun.site/lecture/python/py_example/chp16/score1.xlsx #(1)重點: #正向排序:df.sort_values(by="math") #逆向排序:df.sort_values(by="math", ascending=False) #數學同分則比英文,英文同分則比國文: df.sort_values(by=["math","eng","chi"]) #將缺值(NaN)會放到最上面:df.sort_values(by="math", na_position='first') #------------------------------ #1.練習1:分析學生成績檔(excel) #--------------------- #(1)讀取網路檔案 import pandas as pd df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score3.xlsx","mad3a") #--------------------- #(2)以數學分數正向排序:df.sort_values(by="math") print() print("2. 還沒有排序前的序位=\n", df) print() print("以數學分數正向排序=\n", df.sort_values(by="math")) #--------------------- #(3)以數學分數逆向排序:df.sort_values(by="math", ascending=False) print() print("3. 以數學分數逆向排序=\n", df.sort_values(by="math", ascending=False)) print() #--------------------- #(4)讀取網路檔案:有同分情況下的排序(score3.xlsx) import pandas as pd df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score3.xlsx","mad3a") print() print("4. 還沒有排序前的序位=\n", df) print() print("以數學分數正向排序,3位數學同分=\n", df.sort_values(by="math")) #--------------------- #(5)數學同分則比英文,英文同分則比國文: df.sort_values(by=["math","eng","chi"]) print() print("5. 數學同分則比英文,英文同分則比國文=\n", df.sort_values(by=["math","eng","chi"])) print() print("由高排到低分,數學同分則比英文,英文同分則比國文=\n", df.sort_values(by=["math","eng","chi"], ascending=False)) #--------------------- #(6)讀取網路檔案:有缺值(NaN)下的排序(score1.xlsx) import pandas as pd df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score1.xlsx","mad3a") print() print("6. 還沒有排序前的序位=\n", df) print() print("以數學分數正向排序(預設缺值(NaN)會放到最後)=\n", df.sort_values(by="math")) #--------------------- #(7)將缺值(NaN)會放到最上面:na_position='first' print() print("7. 以數學分數正向排序(將缺值(NaN)會放到最上面)=\n", df.sort_values(by="math", na_position='first')) #-----------------------------------------------------