1. 程式人生 > >吳裕雄 實戰PYTHON編程(8)

吳裕雄 實戰PYTHON編程(8)

www. 取數 global sce title ria val 找到 數值

import pandas as pd

df = pd.DataFrame( {"林大明":[65,92,78,83,70], "陳聰明":[90,72,76,93,56], "黃美麗":[81,85,91,89,77], "熊小娟":[79,53,47,94,80] } )
print(df)

技術分享圖片

import pandas as pd

datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print(df)

技術分享圖片

import pandas as pd

datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
indexs[0] = "林晶輝"
df.index = indexs
columns[3] = "理化"
df.columns = columns
print(df)

技術分享圖片

import pandas as pd
datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print(‘df["自然"] ->‘)
print(df["自然"])
print()
print(‘df[["語文", "數學", "自然"] ->‘)
print(df[["語文", "數學", "自然"]])
print()
print(‘df[df.數學>=80] ->‘)
print(df[df.數學 >= 80])

技術分享圖片

import pandas as pd

datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print("df.values:")
print(df.values)
print("陳聰明的成績(df.values[1]):")
print(df.values[1])
print("陳聰明的英文成績(df.values[1][2]):")
print(df.values[1][2])

技術分享圖片

import pandas as pd

datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print(df)
print(‘df.loc["陳聰明", :] ->‘)
print(df.loc["陳聰明", :])
#print(df.loc["陳聰明"])
print()
print(‘df.loc["陳聰明"]["數學"] ->‘)
print(df.loc["陳聰明"]["數學"])
print()
print(‘df.loc[("陳聰明", "熊小娟") ->‘)
print(df.loc[("陳聰明", "熊小娟"), :])
print()
print(‘df.loc[:, "數學"] ->‘)
print(df.loc[:, "數學"])
print()
print(‘df.loc[("陳聰明", "熊小娟"), ("數學", "自然")] ->‘)
print(df.loc[("陳聰明", "熊小娟"), ("數學", "自然")])
print()
print(‘df.loc["陳聰明":"熊小娟", "數學":"社會"] ->‘)
print(df.loc["陳聰明":"熊小娟", "數學":"社會"])
print()
print(‘df.loc[:黃美麗, "數學":"社會"] ->‘)
print(df.loc[:"黃美麗", "數學":"社會"])
print()
print(‘df.loc["陳聰明":, "數學":"社會"] ->‘)
print(df.loc["陳聰明":, "數學":"社會"])

技術分享圖片

import pandas as pd

datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print(df)
print(‘df.iloc[1, :] ->‘)
print(df.iloc[1, :])
print()
print(‘df.iloc[1][1] ->‘)
print(df.iloc[1][1])

技術分享圖片

import pandas as pd
datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print(df)
print(‘陳聰明的數學科成績 ->‘)
print(df.ix["陳聰明"]["數學"])
print(df.ix["陳聰明"][1])
print(df.ix[1]["數學"])
print(df.ix[1][1])

技術分享圖片

import pandas as pd

datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print(df)
print(‘最前 2 位學生成績 ->‘)
print(df.head(2))
print()
print(‘最後 2 位學生成績 ->‘)
print(df.tail(2))

技術分享圖片

import pandas as pd
datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print(df)
print(‘df.ix["陳聰明"]["數學"] (原始):‘ + str(df.loc["陳聰明"]["數學"]))
df.ix["陳聰明"]["數學"] = 91
print(‘df.ix["陳聰明"]["數學"] (修改):‘ + str(df.loc["陳聰明"]["數學"]))
print()
print(‘df.ix["陳聰明", :] ->‘)
df.ix["陳聰明", :] = 80
print(df.ix["陳聰明", :])

技術分享圖片

import pandas as pd

datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print(df)
print(‘按照數學成績降序排序 ->‘)
df1 = df.sort_values(by="數學", ascending=False)
print(df1)
print()
print(‘按照列標題升序排序 ->‘)
df2 = df.sort_index(axis=0)
print(df2)
print()

技術分享圖片

import pandas as pd
datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
print(df)
print(‘刪除陳聰明成績 ->‘)
df1 = df.drop("陳聰明")
print(df1)
print()
print(‘刪除數學成績 ->‘)
df2 = df.drop("數學", axis=1)
print(df2)
print()
print(‘刪除數學及自然成績 ->‘)
df3 = df.drop(["數學", "自然"], axis=1)
print(df3)
print()
print(‘刪除從陳聰明到熊小娟成績 ->‘)
df4 = df.drop(df.index[1:4])
print(df4)
print()
print(‘刪除從數學到自然的成績 ->‘)
df5 = df.drop(df.columns[1:4], axis=1)
print(df5)
print()

技術分享圖片

import pandas as pd

dt = pd.read_html("http://www.86pm25.com/city/beijing.html")
data=dt[0]
print(data)

技術分享圖片

import pandas as pd

tables = pd.read_html("http://value500.com/M2GDP.html")
n = 1
for table in tables:
print("第 " + str(n) + " 個表格:")
print(table.head())
print()
n += 1

import pandas as pd

tables = pd.read_html("http://value500.com/M2GDP.html")
table = tables[18]
table = table.drop(table.index[0:1])
table.columns = ["年份", "M2指標", "GDP絕對額", "M2/GDP"]
table.index = range(len(table.index))
print(table)

import pandas as pd
from pylab import *
rcParams[‘font.sans-serif‘] = [‘SimHei‘] #設置中文顯示
datas = [[65,92,78,83,70], [90,72,76,93,56], [81,85,91,89,77], [79,53,47,94,80]]
indexs = ["林大明", "陳聰明", "黃美麗", "熊小娟"]
columns = ["語文", "數學", "英文", "自然", "社會"]
df = pd.DataFrame(datas, columns=columns, index=indexs)
df.plot()

技術分享圖片

def rbCity(): #單擊區縣按鈕的處理函數
global sitelist, listradio
sitelist.clear() #清除原有監測站點列表
for r in listradio: #刪除原有監測站點按鈕
r.destroy()
n=0
for c1 in data["監測站點"]: #逐一取出所選區縣市的監測站點
if(c1 == city.get()):
sitelist.append(data.ix[n, 1])
n += 1
sitemake() #生成測站點按鈕
rbSite() #顯示PM2.5數值

def rbSite(): #單擊監測站按鈕後的處理函數
n = 0
for s in data.ix[:,1]: #逐一取得監測站點
if(s == site.get()): #如果某監測站點名稱與選中的監測站點相同,則
pm = data.ix[n][ "PM2.5濃度"] #取得該站點的PM2.5數值
print(pm)
pm=pm[:-5] #去除數據後面的5位單位字符
pm=int(pm) #把PM2.5的字符型數據轉為整型
if(pd.isnull(pm)): #如果沒有數據,則
result1.set(s + "站的 PM2.5 值當前無數據!") #顯示無數據
else: #如果有數據,則
if(pm <= 35): #轉換為空氣質量等級
grade1 = "優秀"
elif(pm <= 53):
grade1 = "良好"
elif(pm <= 70):
grade1 = "中等"
else:
grade1 = "差"
result1.set(s + "站的 PM2.5 值為" + str(pm) + ";" + grade1 )
break #找到選中的監測站點的數據後就跳出循環
n += 1

def clickRefresh(): #重新讀取數據
global data
df = pd.read_html("http://www.86pm25.com/city/beijing.html")
data=df[0]
rbSite() #更新監測站點的數據

def sitemake(): #建立監測站點按鈕
global sitelist, listradio
for c1 in sitelist: #逐一建立按鈕
rbtem = tk.Radiobutton(frame2, text=c1, variable=site, value=c1, command=rbSite) #建立單選按鈕
listradio.append(rbtem) #插入至按鈕列表
if(c1==sitelist[0]): #默認選取第1個按鈕
rbtem.select()
rbtem.pack(side="left") #靠左對齊

import tkinter as tk
import pandas as pd

df = pd.read_html("http://www.86pm25.com/city/beijing.html")
data=df[0]
win=tk.Tk()
win.geometry("640x270")
win.title("PM2.5 實時監測")
city = tk.StringVar() #區縣名稱變量
site = tk.StringVar() #監測站點名稱變量
result1 = tk.StringVar() #顯示信息變量
citylist = [] #區縣列表
sitelist = [] #監測站點列表
listradio = [] #區縣按鈕列表
#建立區縣列表
for c1 in data["監測站點"]:
if(c1 not in citylist): #如果列表中不存在該縣區就將該縣區名稱插入列表
citylist.append(c1)
#建立第1個區縣的監測站點列表
count = 0
for c1 in data["監測站點"]:
if(c1 == citylist[0]): #如果是第1個區縣,則
sitelist.append(data.ix[count, 1]) #把該區縣的所有監測站點插入到監測站點列表
count += 1
label1 = tk.Label(win, text="區縣:", pady=6, fg="blue", font=("新細明體", 12))
label1.pack()
frame1 = tk.Frame(win) #區縣容器
frame1.pack()
for i in range(0,2): #按鈕分2行
for j in range(0,8): #每行8個
n = i * 8 + j #第n個按鈕
if(n < len(citylist)):
city1 = citylist[n] #取得區縣名稱
rbtem = tk.Radiobutton(frame1, text=city1, variable=city, value=city1, command=rbCity) #建立單選按鈕
rbtem.grid(row=i, column=j) #設置按鈕的位置
if(n==0): #選取第1個區縣
rbtem.select()
label2 = tk.Label(win, text="監測站點:", pady=6, fg="blue", font=("新細明體", 12))
label2.pack()
frame2 = tk.Frame(win) #監測站點容器
frame2.pack()
sitemake()
btnDown = tk.Button(win, text="更新數據", font=("新細明體", 12), command=clickRefresh)
btnDown.pack(pady=6)
lblResult1 = tk.Label(win, textvariable=result1, fg="red", font=("新細明體", 16))
lblResult1.pack(pady=6)
rbSite() #顯示測站訊息
win.mainloop()

def rbCity(): #點選縣市選項按鈕後處理函式
global sitelist, listradio
sitelist.clear() #清除原有測站串列
for r in listradio: #移除原有測站選項按鈕
r.destroy()
n=0
for c1 in data["County"] == city.get(): #逐一取出選取縣市的測站
if(c1 == True):
sitelist.append(data.ix[n, 0])
n += 1
sitemake() #建立測站選項按鈕
rbSite() #顯示PM2.5訊息

def rbSite(): #點選測站選項按鈕後處理函式
n = 0
for s in data.ix[:, 0]: #逐一取得測站
if(s == site.get()): #取得點選的測站
pm = data.ix[n, "PM2.5"] #取得PM2.5的值
if(pd.isnull(pm)): #如果沒有資料
result1.set(s + "站的 PM2.5 值目前無資料!")
else: #如果有資料
if(pm <= 35): #轉換為等級
grade1 = "低"
elif(pm <= 53):
grade1 = "中"
elif(pm <= 70):
grade1 = "高"
else:
grade1 = "非常高"
result1.set(s + "站的 PM2.5 值為「" + str(pm) + "」:「" + grade1 + "」等級")
break #找到點選測站就離開迴圈
n += 1

def clickRefresh(): #重新讀取資料
global data
# data = pd.read_csv("http://opendata.epa.gov.tw/ws/Data/REWXQA/?$orderby=SiteName&$skip=0&$top=1000&format=csv")
data = pd.read_csv("F:\\pythonBase\\pythonex\\ch09\\AQX_20160927145712.csv")
rbSite() #更新測站資料

def sitemake(): #建立測站選項按鈕
global sitelist, listradio
for c1 in sitelist: #逐一建立選項按鈕
rbtem = tk.Radiobutton(frame2, text=c1, variable=site, value=c1, command=rbSite) #建立選項按鈕
listradio.append(rbtem) #加入選項按鈕串列
if(c1==sitelist[0]): #預設選取第1個項目
rbtem.select()
rbtem.pack(side="left") #靠左排列

import tkinter as tk
import pandas as pd

# data = pd.read_csv("http://opendata.epa.gov.tw/ws/Data/REWXQA/?$orderby=SiteName&$skip=0&$top=1000&format=csv")
data = pd.read_csv("F:\\pythonBase\\pythonex\\ch09\\AQX_20160927145712.csv")

win=tk.Tk()
win.geometry("640x270")
win.title("PM2.5 實時監測")

city = tk.StringVar() #縣市文字變數
site = tk.StringVar() #測站文字變數
result1 = tk.StringVar() #訊息文字變數
citylist = [] #縣市串列
sitelist = [] #鄉鎮串列
listradio = [] #鄉鎮選項按鈕串列

#建立縣市串列
for c1 in data["County"]:
if(c1 not in citylist): #如果串列中無該縣市就將其加入
citylist.append(c1)
#建立第1個縣市的測站串列
count = 0
for c1 in data["County"]:
if(c1 == citylist[0]): #是第1個縣市的測站
sitelist.append(data.ix[count, 0])
count += 1

label1 = tk.Label(win, text="縣市:", pady=6, fg="blue", font=("新細明體", 12))
label1.pack()
frame1 = tk.Frame(win) #縣市容器
frame1.pack()
for i in range(0,3): #3列選項按鈕
for j in range(0,8): #每列8個選項按鈕
n = i * 8 + j #第n個選項按鈕
if(n < len(citylist)):
city1 = citylist[n] #取得縣市名稱
rbtem = tk.Radiobutton(frame1, text=city1, variable=city, value=city1, command=rbCity) #建立選項按鈕
rbtem.grid(row=i, column=j) #設定選項按鈕位置
if(n==0): #選取第1個縣市
rbtem.select()

label2 = tk.Label(win, text="測站:", pady=6, fg="blue", font=("新細明體", 12))
label2.pack()
frame2 = tk.Frame(win) #測站容器
frame2.pack()
sitemake()

btnDown = tk.Button(win, text="更新資料", font=("新細明體", 12), command=clickRefresh)
btnDown.pack(pady=6)
lblResult1 = tk.Label(win, textvariable=result1, fg="red", font=("新細明體", 16))
lblResult1.pack(pady=6)
rbSite() #顯示測站訊息

win.mainloop()

吳裕雄 實戰PYTHON編程(8)