#範例16-8:DataFrame的row列索引鍵取值法(df.loc[['peter','mike','john'],['chi','eng','math']]),df.loc['peter':'john','chi':'math'] #https://www.runoob.com/pandas/pandas-dataframe.html #https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html #(1)檔案:學生成績檔(excel) #https://acupun.site/lecture/python/py_example/chp16/score2.xlsx #(1)重點1: # row列索引鍵取值法,: # (A).格式1:df.loc[][] # (B).格式2:df.loc[,] #(2)重點2: # row列索引鍵取值法,: # (A).格式1:df.loc[['peter','mike','john'],['chi','eng','math'] # (B).格式2:df.loc['peter':'john', 'chi':'math'] # 說明: # (A).若要用'peter':'john',只能用格式2,不能用格式1 #------------------------------ #1.練習1:分析學生成績檔(excel) #--------------------- #(1)讀取網路檔案 #重點:讀入score1.xlsx資料的時候,必須指定index_col=欄位數(1),以第2個column,作為列row的名稱 #讀入:score1.xlsx(index_col=1,表示以第2個columns作為索引),(第2個columns:peter,mike.....) #--------------------- #(2)如何讀取DataFrame裡2維表格的值(2): df.loc[['peter','mike','john']][['chi','eng','math']] # df.loc[['peter','mike','john'],['chi','eng','math']] print("2. 第1,2,4位同學的數學=\n") #重點1:若是要印出1維資料,['math']:則會取出1維的Series資料 #重點2:若是要印出2維資料,[['chi','eng','math']] = [[.....]]則會取出2維的DataFrame資料 print() print("第1,2,4位同學的國文,英文,數學=\n") print() print("第1,2,4位同學的國文,英文,數學=\n") print() #--------------------- #(3)印出第1~4位資料(國文,英文,數學):df.loc["peter":"john"], # df.loc['peter':'john','chi':'math'] print() print("3. 第1~4位資料('peter':'john')=\n") print() print("第1~4位資料(國文,英文,數學)=\n") print() #錯誤寫法 #print("4. 第1~4位資料(國文,英文,數學)=\n", df.loc[['peter':'john'],['chi':'math']]) #----------------------------------------------------- #----------------------------------------------------- #範例16-8:DataFrame的row列索引鍵取值法(df.loc[['peter','mike','john'],['chi','eng','math']]),df.loc['peter':'john','chi':'math'] #https://www.runoob.com/pandas/pandas-dataframe.html #https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html #(1)檔案:學生成績檔(excel) #https://acupun.site/lecture/python/py_example/chp16/score2.xlsx #(1)重點1: # row列索引鍵取值法,: # (A).格式1:df.loc[][] # (B).格式2:df.loc[,] #(2)重點2: # row列索引鍵取值法,: # (A).格式1:df.loc[['peter','mike','john'],['chi','eng','math'] # (B).格式2:df.loc['peter':'john', 'chi':'math'] # 說明: # (A).若要用'peter':'john',只能用格式2,不能用格式1 #------------------------------ #1.練習1:分析學生成績檔(excel) #--------------------- #(1)讀取網路檔案 #重點:讀入score1.xlsx資料的時候,必須指定index_col=欄位數(1),以第2個column,作為列row的名稱 #讀入:score1.xlsx(index_col=1,表示以第2個columns作為索引),(第2個columns:peter,mike.....) import pandas as pd df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score2.xlsx","mad3a",index_col=1) #--------------------- #(2)如何讀取DataFrame裡2維表格的值(2): df.loc[['peter','mike','john']][['chi','eng','math']] # df.loc[['peter','mike','john'],['chi','eng','math']] print("2. 第1,2,4位同學的數學=\n", df.loc[['peter','mike','john']]['math']) #重點1:若是要印出1維資料,['math']:則會取出1維的Series資料 #重點2:若是要印出2維資料,[['chi','eng','math']] = [[.....]]則會取出2維的DataFrame資料 print() print("第1,2,4位同學的國文,英文,數學=\n", df.loc[['peter','mike','john']][['chi','eng','math']]) print() print("第1,2,4位同學的國文,英文,數學=\n", df.loc[['peter','mike','john'],['chi','eng','math']]) print() #--------------------- #(3)印出第1~4位資料(國文,英文,數學):df.loc["peter":"john"], # df.loc['peter':'john','chi':'math'] print() print("3. 第1~4位資料('peter':'john')=\n", df.loc["peter":"john"]) print() print("第1~4位資料(國文,英文,數學)=\n", df.loc['peter':'john','chi':'math']) print() #錯誤寫法 #print("4. 第1~4位資料(國文,英文,數學)=\n", df.loc[['peter':'john'],['chi':'math']]) #-----------------------------------------------------