Pandas入門之八:字串與文字資料
阿新 • • 發佈:2021-07-14
已信任 Jupyter 伺服器: 本地 Python 3: Not Started [13] import pandas as pd import numpy as np [15] s = pd.Series([' Tom',' xiaoming','john ']) s 0 Tom 1 xiaoming 2 john dtype: object [16] # 刪除空格 s.str.strip() 0 Tom 1 xiaoming 2 john dtype: object [17]# 字元分割 s.str.split('o') 0 [ T, m] 1 [ xia, ming] 2 [j, hn ] dtype: object [18] # 字元拼接 s.str.cat(sep='<=>') ' Tom<=> xiaoming<=>john ' [19] # 獲取onehot編碼 s.str.get_dummies() Tom xiaoming john 0 1 0 0 1 0 1 0 2 0 0 1 [20]# 字串是否包含什麼內容 s.str.contains('m') 0 True 1 True 2 False dtype: bool [21] # 字串替換 s.str.replace('o','aaa') 0 Taaam 1 xiaaaaming 2 jaaahn dtype: object [22] # 計數 s.str.count('i') 0 0 1 2 2 0 dtype: int64 [33] s = pd.Series(['348',' 697 xiaoming','john']) s 0 348 1 697 xiaoming 2 john dtype: object [34] # 判斷字串是否為數字 s.str.isnumeric() 0 True 1 False 2 False dtype: bool [-] [-]