Skip to content

Instantly share code, notes, and snippets.

@ting11222001
Created June 13, 2020 15:41
Show Gist options
  • Save ting11222001/bd98f6b3972c1627fc540810b0db16cc to your computer and use it in GitHub Desktop.
Save ting11222001/bd98f6b3972c1627fc540810b0db16cc to your computer and use it in GitHub Desktop.
#載入套件
#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