# 0411
https://www.learncodewithmike.com/2020/11/python-pandas-dataframe-tutorial.html
### 上課筆記
```
容器:串列 字典 panda
```

```
# 手工掛載雲端硬碟
# 寫入CSV #'/路徑///資料夾/檔名.副檔名(txt)', 'w(寫入)'
with open('/content/drive/MyDrive/___DataSet/001_Hello.txt', 'w') as f:#以打開檔案為基礎 並把檔案命名為f
f.write('Hello Google Drive 溫瑩瑄') #把()寫入f檔案
```
```
# 讀取CSV
with open('/content/drive/MyDrive/___DataSet/001_Hello.txt', 'r') as f:
ss=f.read() #r(讀取)
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("檔案不存在") #如果上面的if不成立就跑else如果上面的if不成立就跑else
檔案已經刪除
```
```
#讀取雲端硬碟中的excel檔
import pandas as pd #pd命名
df = pd.read_excel('/content/drive/My Drive/___DataSet/001_Income_F.xlsx')
df
```

```
#讀取雲端硬碟中的excel檔
import pandas as pd
data = pd.read_excel("/content/drive/My Drive/___DataSet/110 學年度全國大專校院及校長名錄(含學校本部地址).xlsx")
data
159 rows × 14 columns
```

```
import pandas as pd
data = pd.read_excel("/content/drive/My Drive/___DataSet/程設0411.xlsx")
data
```

```
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)
df
```

```
grades = [
["Mike", 80, 63],
["Sherry", 75, 90],
["Cindy", 93, 85],
["John", 86, 70]
]
new_df = pd.DataFrame(grades)
print("使用陣列來建立df:")
print(new_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)
df
```

```
head():取得最前面的n筆資料,並且會回傳一個新的Pandas
```
```
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("=================================")
new_df = df.head(2)
print("取得最前面的兩筆資料")
print(new_df)
print("=================================")
print("取得單一欄位資料(型別為DataFrame)")
print(df[["name"]])
print("=================================")
print("取得多欄位資料(型別為DataFrame)")
print(df[["name", "chinese"]]) #[[串列]]
print("=================================")
print("取得索引值0~2的資料")
print(df[0:3])
print("=================================")
print("利用at()方法取得索引值為1的math欄位資料")
print(df.at[1, "math"])
print("=================================")
print("利用iat()方法取得索引值為1的第一個欄位資料")
print(df.iat[1, 2]) #先切列在切欄
print("=================================")
print("取得資料索引值為1和3的name及chinese欄位資料集")
print(df.loc[[1, 3], ["name", "chinese"]])
print("=================================")
print("取得資料索引值為1和3的第一個及第三個欄位資料集")
print(df.iloc[[1, 3], [0, 2]]) #1.sherry2.john列,0name 2chhinese欄
```



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

```
for i in range(0,5):
for j in range(0,5):
print(f'(i),(j)')
print("=============")
print(df.at[i, "編號"])
(i),(j)
=============
A001
(i),(j)
=============
A001
(i),(j)
=============
A001
(i),(j)
=============
A001
(i),(j)
=============
A001
(i),(j)
=============
A002
(i),(j)
=============
A002
(i),(j)
=============
A002
(i),(j)
=============
A002
(i),(j)
=============
A002
(i),(j)
=============
A003
(i),(j)
=============
A003
(i),(j)
=============
A003
(i),(j)
=============
A003
(i),(j)
=============
A003
(i),(j)
=============
A004
(i),(j)
=============
A004
(i),(j)
=============
A004
(i),(j)
=============
A004
(i),(j)
=============
A004
(i),(j)
=============
A005
(i),(j)
=============
A005
(i),(j)
=============
A005
(i),(j)
=============
A005
(i),(j)
=============
A005
```
{"metaMigratedAt":"2023-06-16T22:55:24.820Z","metaMigratedFrom":"Content","title":"0411","breaks":true,"contributors":"[{\"id\":\"4dfebb48-62e3-4093-b22e-bde00bb0bc86\",\"add\":4375,\"del\":0}]"}