在資料收集的過程中,我們會發現 App Store 與 Google Play 的爬蟲結果雖然都是評論資料,但它們的結構往往不同。例如:
title
, content
, rating
, updated
userName
, content
, score
, at
這樣的情況下,若要後續進行分析,就會遇到欄位名稱與結構對不上,難以直接合併。
因此,今天的任務是:將不同來源的資料,轉換成統一的格式。
我們設定以下五個欄位作為標準格式:
title
→ 評論標題(若無標題,則填入空字串 ""
)review
→ 評論內容rating
→ 評分(數字 1–5)date
→ 評論日期platform
→ 資料來源("App Store"
或 "Google Play"
)import pandas as pd
# 轉換 App Store 格式
df_app_store = pd.DataFrame(app_store_data)
df_app_store = df_app_store.rename(columns={
"content": "review",
"updated": "date"
})
df_app_store["platform"] = "App Store"
# 轉換 Google Play 格式
df_google_play = pd.DataFrame(google_play_data)
df_google_play = df_google_play.rename(columns={
"content": "review",
"score": "rating",
"at": "date"
})
df_google_play["title"] = "" # Google Play 無標題
df_google_play["platform"] = "Google Play"
df_google_play = df_google_play[["title", "review", "rating", "date", "platform"]]
# App Store 保留相同欄位順序
df_app_store = df_app_store[["title", "review", "rating", "date", "platform"]]
df_google_play
# df_app_store
到這裡,我們已經把兩種資料整理成一致的欄位格式,為資料合併做好準備。
下方圖片為整理好的資料表內容