---
# System prepended metadata

title: 個人第七堂課

---

# 個人第七堂課

``` python=
#寫入雲端硬碟
from google.colab import drive
drive.mount('/content/drive')
```
``` python=
# 寫入CSV
with open('/content/drive/MyDrive/___DataSet/001_Hello.txt', 'w') as f:
  f.write('Hello Google Drive ')
```
``` python=
# 讀取CSV
with open('/content/drive/MyDrive/___DataSet/001_Hello.txt', 'r') as f:  
  ss=f.read()
  print(ss)
```    
``` python=
#讀取雲端硬碟中的excel檔
import os
os.chdir('/content/drive/My Drive/___DataSet') 
os.listdir() #確認目錄內容
```
``` python=
import pandas as pd
data = pd.read_excel(“/content/drive/My Drive/___DataSet (1)/110 學年度全國大專校院及校長名錄(含學校本部地址).xlsx”)
data

```

``` python=
import pandas as pd
 
 
grades = {
    "name": ["Mike", "Sherry", "Cindy", "John"],
    "math": [80, 75, 93, 86],
    "chinese": [63, 90, 85, 70]
}
 
df = pd.DataFrame(grades)
 
print("使用字典來建立df：")
print(df)
 
print("=====================")
 
grades = [
    ["Mike", 80, 63],
    ["Sherry", 75, 90],
    ["Cindy", 93, 85],
    ["John", 86, 70]
]
 
new_df = pd.DataFrame(grades)
 
print("使用陣列來建立df：")
print(new_df)
```

``` python=
import pandas as pd
 
 
grades = {
    "學號": ["108251037", "108251036", "108251035", "108251034"],
    "體重": [70, 75, 93, 86],
    "身高": [183, 190, 185, 170]
}
 
df = pd.DataFrame(grades)
df.index = ["1列", "2列", "3列", "4列"]  #自訂索引值
df.columns = ["學號", "體重", "身高"]  #自訂欄位名稱
print(df)

```

``` python=
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：")
df
```

``` python=
df.at[4, "身高"]
```

``` python=
df.loc[[1,0,4],[ "體重","學號"]] #指定數列生成新表格
```

``` python=
# range應用
list (range(1,10))
```

``` python=
# range應用帶入df
for i in range(0,5):
    print(i)
    print(df.at[i,'學號'])

```
:::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
:::

