我有一個如下所示的資料框。
df =
A B
1.0 4.0 45.0
2.0 4.0 11.2
3.0 49.2 10.8
當我使用選擇特定行時loc:
df.loc[1.0][['A','B']]
上面的代碼行結果如下:
A 4.0
B 45.0
Name: 1.0, dtype: object
我想在一個元組中表示這些值。因此,我執行以下操作:
tuple(df.loc[1.0][['A','B']])
這給了我一個結果('4.0', '45.0'),表明元組內的值是字串型別。如何將其轉換為浮動?那就是我希望結果看起來像
(4.0, 45.0)。
我試圖在將其表示為元組之前轉換該值。例如: df.loc[1.0]['A'].astype(float64)它導致以下錯誤。
AttributeError: 'str' object has no attribute 'astype'.
uj5u.com熱心網友回復:
因為df.loc[1.0, ['A','B']]有Series可能將值更改為浮點數:
tuple(df.loc[1.0, ['A','B']].astype(float))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/484955.html
