參考: 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如何串接OpenAI /Claude /Gemini API自動將大量維修紀錄JSON轉自然語言描述(並避免中斷資料遺失)response = client.chat.completions.create() ; reply = response.choices[0].message.content Python如何串接OpenAI /Claude /Gemini API自動將大量維修紀錄JSON轉自然語言描述(並避免中斷資料遺失)response = client.chat.completions.create() ; reply = response.choices[0].message.content](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2025/07/20250716084059_0_c5b368.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



近期留言