參考: Python: pandas.DataFrame.astype函数方法的使用

舉例:

df1.astype( dtype = np.float64, errors = “ignore”)

df1.astype(dtype= np.float64)
#沒有設定errors參數,預設值為”raise”
ValueError: could not convert string to float: ‘Freq_12000_MHz_H_Amp’
則只能對1D 的資料做處理
若對 DataFrame處理
會出現
TypeError: arg must be a list, tuple, 1-d array, or Series
numpy.ndarray的astype() :

容易誤會 str(a)能將
array所有元素轉為字串
需使用 a.astype(str)
a.astype(object) 也可
a.astype(np.object)雖然可以跑
但會出現np.object已經廢棄的警告訊息
建議單獨使用object即可
推薦hahow線上學習python: https://igrape.net/30afN


![Python 進階技巧:海象運算子 (Walrus Operator) 實戰教學 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS] Python 進階技巧:海象運算子 (Walrus Operator) 實戰教學 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS]](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2026/02/20260210083748_0_a7d9bf.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![使用 Python 檢驗字符串格式:掌握正則表達式(Regular Expression)的起始^與終止$符號, pattern = r’^GATR[0-9]{4}$’ 使用 Python 檢驗字符串格式:掌握正則表達式(Regular Expression)的起始^與終止$符號, pattern = r’^GATR[0-9]{4}$’](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2024/07/20240712093637_0.png?quality=90&zoom=2&ssl=1&resize=350%2C233)






近期留言