取代單一值 I
在這個練習中,我們要在另一個資料集上,套用以字典一次取代多個值的技巧。
我們會在 DataFrame data 上操作。每一列代表一副撲克牌中 5 張牌的點數,從 1(Ace)到 13(King)(特徵為 R1、R2、R3、R4、R5)。特徵「Class」將每一列分類為 0 到 9 的類別,而特徵「Explanation」則簡要說明各類別的意義。
本練習的目標是把撲克中的兩種同花牌型('Royal flush' 與 'Straight flush')統一歸類到名稱 'Flush' 底下。
本練習屬於課程
使用 pandas 撰寫高效程式碼
練習說明
- 將 DataFrame 中「Explanation」欄位標示為
'Royal flush'或'Straight flush'的每一列都改為'Flush'。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Replace Royal flush or Straight flush to Flush
poker_hands.replace({____:____, ____:____}, inplace=True)
print(poker_hands['Explanation'].head())