# 第七堂課 ****課堂筆記**** # 手工掛載雲端硬碟 # 寫入CSV with open('/content/drive/MyDrive/___DataSet/001_Hello.txt', 'w') as f: f.write('Hello Google Drive') # 讀取CSV with open('/content/drive/MyDrive/___DataSet/001_Hello.txt', 'r') as f: ss=f.read() print(ss) Hello Google Drive # 刪除檔案 import os if os.path.exists("/content/drive/MyDrive/___DataSet//001_Hello.txt"): os.remove("/content/drive/MyDrive/___DataSet//001_Hello.txt") print("檔案已經刪除") else: print("檔案不存在") # 顯示雲端硬碟 import os os.chdir('/content/drive/My Drive/___DataSet') os.listdir() #目錄內容 ['Untitled0.ipynb', 'Untitled1.ipynb', 'Untitled2.ipynb', 'Untitled3.ipynb', 'Untitled4.ipynb', 'Untitled5.ipynb', '001_Hello.txt'] # 讀取雲端硬碟中的excel檔 import pandas as pd df = pd.read_excel('/content/drive/My Drive/___DataSet/001_Income_F.xlsx') df ID _id Income Distribution 0 A50618 6224c4f943717d05bef45e53 0 F 1 A19524 6224c4f943717d05bef3e4dd 1 F 2 A07790 6224c4f943717d05bef3b707 1 F 3 A11069 6224c4f943717d05bef3c3d6 1 F 4 A32397 6224c4f943717d05bef41726 1 F ... ... ... ... ... 99995 A45819 6224c4f943717d05bef44b94 337748 F 99996 A97996 6224c4fa43717d05bef51765 373315 F 99997 A32096 6224c4f943717d05bef415f9 377986 F 99998 A23399 6224c4f943717d05bef3f400 381208 F 99999 A54841 6224c4f943717d05bef46ed2 381605 F 100000 rows × 4 columns # 讀取雲端硬碟中的excel檔 import pandas as pd data = pd.read_excel("/content/drive/My Drive/___DataSet/110 學年度全國大專校院及校長名錄(含學校本部地址).xlsx") data 學校\n代碼 公私立 體制 學校名稱 學校英文名稱 職稱 姓名 縣市別 第三級\n行政區 郵遞\n區號 學校地址 學校總機 學校傳真 網址 0 0001 公立 一般大學 國立政治大學 National Chengchi University 校長 郭明政 臺北市 文山區 11605 臺北市文山區指南路2段64號 02-2939-3091 02-2937-9611 http://www.nccu.edu.tw 1 0002 公立 一般大學 國立清華大學 National Tsing Hua University 校長 賀陳弘 新竹市 新竹市 30013 新竹市光復路2段101號 03-571-5131 03-572-4038 http://www.nthu.edu.tw 2 0003 公立 一般大學 國立臺灣大學 National Taiwan University 校長 管中閔 臺北市 大安區 10617 臺北市大安區羅斯福路4段1號 02-3366-3366 02-2362-7651 http://www.ntu.edu.tw 3 0004 公立 一般大學 國立臺灣師範大學 National Taiwan Normal University 校長 吳正己 臺北市 大安區 10610 臺北市大安區和平東路1段162號 02-7749-1111 無 http://www.ntnu.edu.tw 4 0005 公立 一般大學 國立成功大學 National Cheng Kung University 校長 蘇慧貞 臺南市 東 區 70101 臺南市東區大學路1號 06-275-7575 06-276-6462 http://www.ncku.edu.tw ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 154 1R07 私立 宗教研修學院 台灣基督長老教會南神神學院 Tainan Theological College 校長 胡忠銘 臺南市 東 區 70142 臺南市東區東門路一段117號 06-237-1291 06-234-6060 http://www.ttcs.edu.tw/ 155 1R08 私立 宗教研修學院 基督教華神學校財團法人中華福音神學研究學院 China Evangelical Graduate School of Theology 校長 戴繼宗 桃園市 八德區 334025 桃園市八德區長安街53號 03-273-7477 03-371-4703 http://wp.ces.org.tw/ 156 1R09 私立 宗教研修學院 唯心聖教學院 Weixin Shengjiao College 校長 陳立岳 南投縣 南投市 540022 南投縣南投市文化路705巷667號 049-220-9418 049-220-3351 http://www.wxc.org.tw/ 157 3002 公立 一般大學 臺北市立大學 University of Taipei 校長 邱英浩\n代理 臺北市 中正區 10048 臺北市中正區愛國西路1號 02-2311-3040 02-2381-4067 http://www.utaipei.edu.tw/ 158 3A01 公立 空中大學 高雄市立空中大學 Open University of Kaohsiung 校長 劉嘉茹 高雄市 小港區 81249 高雄市小港區大業北路436號 07-801-2008 07-806-6725 http://www.ouk.edu.tw 159 rows × 14 columns 處理外部檔案與Pandas DataFrame === * 用「表格」的方式組織資料 自製excel:欄位(column)、列(row)、索引(index) ![](https://i.imgur.com/sukCjv4.png) ![](https://i.imgur.com/L6ZwQj2.png) ![](https://i.imgur.com/dnRCqsE.png) ![](https://i.imgur.com/VLVJ4uC.png) ``` df.iat[4,2] A:170 ``` ``` df.at[1,"身高"] A:157 ``` ``` df.iloc[[1,3],[0,1,2]] ``` ![](https://i.imgur.com/CN7Qy2C.png) ``` df.loc[[1,3],["學號","體重"]] ``` ![](https://i.imgur.com/t2Mz3B4.png) ![](https://i.imgur.com/EM7WWhW.png) ![](https://i.imgur.com/LcAnxUM.png) ![](https://i.imgur.com/0BKZbdo.png) ![](https://i.imgur.com/LA7UlGi.png) ![](https://i.imgur.com/wOd5s55.png) ``` for i in range(1,5): for j in range(0,5): print(f'{i},{j}') A: 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 ``` # 小組作業 ![](https://i.imgur.com/TeRveQb.png) :::warning 1.怎麼橫著印出來 import pandas as pd grades={ "學號":['A001','A002','A003','A004','A005'], "體重":[39,50,60,70,75], "身高":[15,160,190,180,170] } df = pd.DataFrame(grades) print (df. iat[0, 0], df. iat[1, 0], df. iat[2, 0], df. iat[3, 0], df. iat[4, 0]) print (df. iat[0, 1], df. iat[1, 1], df. iat[2, 1], df. iat[3, 1], df. iat[4, 1]) print (df. iat[0, 2], df. iat[1, 2], df. iat[2, 2], df. iat[3, 2], df. iat[4, 2]) A: A001 A002 A003 A004 A005 39 50 60 70 75 15 160 190 180 170 ::: :::info 2.怎麼拿出索引值1的每個欄位(包含用迴圈) import pandas as pd grades = { "學號": ["A001", "A002", "A003", "A004", "A005"], "體重": [60, 50, 80, 75, 72], "身高": [165, 157, 182, 175, 170] } df = pd.DataFrame(grades) for i in range(1,5): if i == 1: print(df.at[i,"學號"],df.at[i,"體重"],df.at[i,"身高"]) A: A002 50 157 ::: :::warning 3.怎麼用雙迴圈印出全部15個值 import pandas as pd grades={ "學號":['A001','A002','A003','A004','A005'], "體重":[39,50,60,70,75], "身高":[15,160,190,180,170] } df = pd.DataFrame(grades) for i in df: for j in df[i]: print(j) A: A001 A002 A003 A004 A005 39 50 60 70 75 15 160 190 180 170 ::: # 會議記錄 ------------------------ * 4/17 ![](https://i.imgur.com/DBGnvc3.jpg)