參考: 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如何做excel的樞紐分析? DataFrame .pivot_table (values=None, index=None, columns=None, aggfunc=’mean’) ; df.groupby([‘A’, ‘B’, ‘C’], sort=False)[‘D’].sum().unstack(‘C’) Python如何做excel的樞紐分析? DataFrame .pivot_table (values=None, index=None, columns=None, aggfunc=’mean’) ; df.groupby([‘A’, ‘B’, ‘C’], sort=False)[‘D’].sum().unstack(‘C’)](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230325141855_86.png?quality=90&zoom=2&ssl=1&resize=350%2C233)


近期留言