參考: 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 pandas.DataFrame去重:df.drop_duplicates() 與 df[~df.duplicated()] 的等價、差異與最佳實踐 一文搞懂Python pandas.DataFrame去重:df.drop_duplicates() 與 df[~df.duplicated()] 的等價、差異與最佳實踐](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2025/08/20250808202701_0_66f9bc.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




近期留言