有一個包含兩列的資料框,其中第一列是整數,第二列是浮點數:
inst weight
0 126400 0.845194
1 4382592 0.083728
如果我使用row = df.iloc[0].astype(float),那么第一列也將轉換為浮點數。我想靜態指定 int 和 float 并將它們分配給變數,例如:
w = df['weight'].iloc[0].astype(float)
i = df['inst'].iloc[0].astype(int)
但這會導致錯誤,例如AttributeError: 'float' object has no attribute 'astype'. 我該如何解決?
uj5u.com熱心網友回復:
您必須將整列設定為型別。
喜歡:
df['weight'] = df['weight'].astype(float)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/429586.html
上一篇:如何使用pythonpandas提取具有一些處理步驟的行?
下一篇:Python中的JSON決議問題
