#----------------------------------------------------- #範例16-12:df的新增,與儲存 #https://www.w3schools.com/python/pandas/ref_df_insert.asp #https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html #https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.insert.html #https://www.geeksforgeeks.org/python-pandas-dataframe-insert/ #https://datagy.io/pandas-add-row/ #(1)檔案:學生成績檔(excel) #https://acupun.site/lecture/python/py_example/chp16/score2.xlsx #(2)重點1:新增有2種方法 # 新增1個欄位法(1欄):df.insert(4,"his",[60,65,75,75,80,85]) # 新增1筆學生資料法(1列):df = df.append({"id":"9007", "name":"張三", "math":70},ignore_index=True) #(3)重點2:新增1個欄位法(1欄) # 指令:df.insert(欄位編號,"欄位名稱",[值1,值2,值3,值4,值5,值5]) #(4)重點3:新增1筆學生資料法(1列): # 指令:df = df.append({"id":"9007", "name":"張三", "math":70},ignore_index=True) #------------------------------ #1.練習1:新增資料:新增1個欄位 #--------------------- #(1)讀取網路檔案:score2.xlsx #import pandas as pd #df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score2.xlsx","mad3a") print() print("1-1.全部的資料=\n") #--------------------- #(2)增加1個欄位:放在第4欄位處:歷史分數his,6個學生分數:df.insert(4,"his",[60,65,75,75,80,85]) print() print("1-2.增加1個欄位:歷史分數his,6個學生=\n") #--------------------- #(3)增加1個欄位:放在第2欄位處:地理分數geo,6個學生分數都是90:df.insert(2,"geo",60) print() print("1-3.增加1個欄位:地理分數geo,6個學生都是90=\n") #------------------------------ #2.練習2:新增資料:1筆學生資料(1列):df = df.append({"id":"9007", "name":"張三", "chi":70, "eng":70, "math":70, "his":70, "geo":70},ignore_index=True) #--------------------- #(1)增加1筆學生資料(1列dict): print() #注意1: 必須df = df.append(),才能新增 # 若是, df.append()無法新增 #注意2: 必須加上參數:ignore_index=True,才能不需要加上index編號,就能新增一筆資料 print("2-1.新增1筆學生資料(1列dict)=\n") #--------------------- #(2)增加1筆學生資料(1列Series):df = df.append(pd.Series(['9008','李四', 10, 20, 30, 40, 50],index=['id','name','geo','chi','eng','his','math']),ignore_index=True) print() #注意1: 必須df = df.append(),才能新增 # 若是, df.append()無法新增 #注意2: 必須加上參數:ignore_index=True,才能不需要加上index編號,就能新增一筆資料 #注意3:pd.Series格式 = pd.Series([..值..], index=[...對應欄位], ignore_index=True) print("2-2.新增1筆學生資料(1列Series)=\n") #-------------------- #(3)把df,儲存為score1c.xlsx print() #(A).儲存到虛擬專案區(暫存幾天) #df.to_excel("score1c.xlsx") #(A).加入google雲端的目錄(永久保存) #A.先在左邊的『檔案』, #B.點按『掛接雲端硬碟』,顯示『drive』 #import os #os.chdir("/content/drive/MyDrive/Colab Notebooks") #df.to_excel("score1c.xlsx") #查看目錄的檔案名稱 #!ls #-------------------------------------------------------------------- #-------------------------------------------------------------------- #範例16-12:df的新增,與儲存 #https://www.w3schools.com/python/pandas/ref_df_insert.asp #https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html #https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.insert.html #https://www.geeksforgeeks.org/python-pandas-dataframe-insert/ #https://datagy.io/pandas-add-row/ #(1)檔案:學生成績檔(excel) #https://acupun.site/lecture/python/py_example/chp16/score2.xlsx #(2)重點1:新增有2種方法 # 新增1個欄位法(1欄):df.insert(4,"his",[60,65,75,75,80,85]) # 新增1筆學生資料法(1列):df = df.append({"id":"9007", "name":"張三", "math":70},ignore_index=True) #(3)重點2:新增1個欄位法(1欄) # 指令:df.insert(欄位編號,"欄位名稱",[值1,值2,值3,值4,值5,值5]) #(4)重點3:新增1筆學生資料法(1列): # 指令:df = df.append({"id":"9007", "name":"張三", "math":70},ignore_index=True) #------------------------------ #1.練習1:新增資料:新增1個欄位 #--------------------- #(1)讀取網路檔案:score2.xlsx import pandas as pd df = pd.read_excel("https://acupun.site/lecture/python/py_example/chp16/score2.xlsx","mad3a") print() print("1-1.全部的資料=\n", df) #--------------------- #(2)增加1個欄位:放在第4欄位處:歷史分數his,6個學生分數:df.insert(4,"his",[60,65,75,75,80,85]) print() df.insert(4,"his",[60,65,75,75,80,85]) print("1-2.增加1個欄位:歷史分數his,6個學生=\n", df) #--------------------- #(3)增加1個欄位:放在第2欄位處:地理分數geo,6個學生分數都是90:df.insert(2,"geo",60) print() df.insert(2,"geo",60) print("1-3.增加1個欄位:地理分數geo,6個學生都是90=\n", df) #------------------------------ #2.練習2:新增資料:1筆學生資料(1列):df = df.append({"id":"9007", "name":"張三", "chi":70, "eng":70, "math":70, "his":70, "geo":70},ignore_index=True) #--------------------- #(1)增加1筆學生資料(1列dict): print() df = df.append({"id":"9007", "name":"張三", "chi":70, "eng":70, "math":70, "his":70, "geo":70},ignore_index=True) #注意1: 必須df = df.append(),才能新增 # 若是, df.append()無法新增 #注意2: 必須加上參數:ignore_index=True,才能不需要加上index編號,就能新增一筆資料 print("2-1.新增1筆學生資料(1列dict)=\n") #--------------------- #(2)增加1筆學生資料(1列Series):df = df.append(pd.Series(['9008','李四', 10, 20, 30, 40, 50],index=['id','name','geo','chi','eng','his','math']),ignore_index=True) print() df = df.append(pd.Series(['9008','李四', 10, 20, 30, 40, 50],index=['id','name','geo','chi','eng','his','math']),ignore_index=True) #注意1: 必須df = df.append(),才能新增 # 若是, df.append()無法新增 #注意2: 必須加上參數:ignore_index=True,才能不需要加上index編號,就能新增一筆資料 #注意3:pd.Series格式 = pd.Series([..值..], index=[...對應欄位], ignore_index=True) print("2-2.新增1筆學生資料(1列Series)=\n", df) #-------------------- #(3)把df,儲存為score1c.xlsx print() #(A).儲存到虛擬專案區(暫存幾天) #df.to_excel("score1c.xlsx") #(A).加入google雲端的目錄(永久保存) #A.先在左邊的『檔案』, #B.點按『掛接雲端硬碟』,顯示『drive』 import os os.chdir("/content/drive/MyDrive/Colab Notebooks") #df.to_excel("score1c.xlsx") #查看目錄的檔案名稱 !ls #--------------------------------------------------------------------