Created
June 13, 2020 15:41
-
-
Save ting11222001/bd98f6b3972c1627fc540810b0db16cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#載入套件 | |
#API: https://www.dcard.tw/service/api/v2/forums | |
import pandas as pd | |
import requests | |
r = requests.get('https://www.dcard.tw/service/api/v2/forums') | |
response = r.text | |
import json | |
data = json.loads(response) | |
#是線上api所以可以直接以json型態下載 | |
#也可以把以上步驟簡略成這行程式碼 | |
#data = r.json() | |
#轉換成dataframe | |
df = pd.DataFrame(data) | |
#印出資料前三行,確認資料載入如同預期 | |
df.head(3) | |
#可以看所有columns | |
df.columns | |
#依序且篩選出「name,isSchool,description,subscriptionCount,createdAt」欄位,並且將欄位命名成中文。 | |
#用兩層中括號篩選 | |
df1 = df[['name', 'isSchool', 'description', 'subscriptionCount', 'createdAt', 'updatedAt']] | |
#可以用.columns選取欄位並重新命名 | |
df1.columns = ['看版名稱', '學校', '描述', '訂閱人數', '建立時間', '更新時間'] | |
df1.head(3) | |
#最新有更動的看板前三名 | |
df2 = df1.sort_values('更新時間', ascending = False) | |
df2.head(3) | |
#最多訂閱人數前三名 | |
df3 = df1.sort_values('訂閱人數', ascending = False) | |
df3.head(3) | |
#只挑出是學校的看版,並且對訂閱人數做排序 | |
mask1 = df1['學校'] == True | |
df5 = df1[mask1].sort_values('訂閱人數', ascending = False) | |
df5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment