---
# System prepended metadata

title: '0411'

---

0411
===

手工掛載雲端硬碟
寫入CSV

with open('/content/drive/MyDrive/___TestData/001_Hello.txt', 'w') as f:
   f.write('Hello Google Drive')

#以打開這個檔案並設定為f為基礎,做以下動作
#/content/drive/MyDrive/___TestData/ 路徑  
#001_Hello.txt 檔名
#'w' 寫入

---
讀取CSV
with open('/content/drive/MyDrive/___TestData/001_Hello.txt', 'r') as f:  
  ss=f.read()
  print(ss)
  
---
刪除檔案 ,匯入os模組
import os
if os.path.exists("/content/drive/MyDrive/___TestData//001_Hello.txt"):
  os.remove("/content/drive/MyDrive/___TestData//001_Hello.txt")
  print("檔案已經刪除")
else:
  print("檔案不存在")

---

from google.colab import drive
drive.mount('/content/drive')

---

import os
os.chdir('/content/drive/My Drive/___TestData') 
os.listdir() #確認目錄內容

---

import pandas as pd
df = pd.read_csv('/content/drive/My Drive/___TestData/opendata110road.csv')
df

----

import pandas as pd
data = pd.read_excel("/content/drive/My Drive/___TestData/110 學年度全國大專校院及校長名錄(含學校本部地址).xlsx")
data

---

import pandas as pd
df = pd.read_csv('/content/drive/My Drive/___TestData/opendata110road.csv')
df

---

import pandas as pd
 
 
grades = {
    "name": ["Mike", "Sherry", "Cindy", "John"],
    "math": [80, 75, 93, 86],
    "chinese": [63, 90, 85, 70]
}
 
df = pd.DataFrame(grades)
df.index = ["s1", "s2", "s3", "s4"]  #自訂索引值
df.columns = ["student_name", "math_score", "chinese_score"]  #自訂欄位名稱
print(df)

---

import pandas as pd #借助pandas模組
grades = {
    "學號": ["A001", "A002", "A003", "A004", "A005"],
    "體重": [60, 50, 80, 75, 72],
    "身高": [165, 157, 182, 175, 170]
}
df = pd.DataFrame(grades)
 
print("使用字典來建立df：")
df

---
import pandas as pd #借助pandas模組
grades = {
    "學號": ["A001", "A002", "A003", "A004", "A005"],
    "體重": [60, 50, 80, 75, 72],
    "身高": [165, 157, 182, 175, 170]
}
df = pd.DataFrame(grades)
 
print(df["身高"])
print(df[ ["身高","體重"] ])
print(df[0:3])
df.at[4,"身高"]

---
import pandas as pd
grades = {
    "學號": [ "A001", "A002", "A003", "A004", "A005"],
    "體重": [60, 50, 80, 75, 72],
    "身高": [165, 157, 182, 175, 170]
}
df = pd.DataFrame(grades)
print(df.iloc[[0],[0,1,2]])

---

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(0,5):
  for j in range(i,i+1):
    for k in range(i,i+1):
      print(f'{df.at[i,"學號"]},{df.at[j,"體重"]}，{df.at[k,"身高"]}')
      
---
import pandas as pd
 
grades = {
    "name": ["Mike", "Sherry", "Cindy", "John"],
    "math": [80, 75, 93, 86],
    "chinese": [63, 90, 85, 70]
}
 
df = pd.DataFrame(grades)
df.index = ["s1", "s2", "s3", "s4"]  #自訂索引值
df.columns = ["student_name", "math_score", "chinese_score"]  #自訂欄位名稱
print(df)