所以我想在Pandas中把一個整數轉換成字串,而不是一整列或一行。然而,Pandas不允許我使用str(i),因為i是一個整數,并且出現了模塊物件不可呼叫的錯誤。 我應該如何解決這個問題呢?
所以我有一個索引i = 5
我想把它追加到一個字串中 str = '[' str(i) ']'/code>
我的期望是str = 1
在此輸入圖片描述
uj5u.com熱心網友回復:
你可以你astype,例如,我創建一個資料框架:
df = pd. DataFrame({"col1":["A","B","C"] 。
"col2":[1,2, 3]})
df.head()
輸出:
col1 col2
0 A 1
1 B 2
2 C 3
并將col2中的值3變成str:
temp = df.col2[2].astype(str)
temp, type(temp)
輸出:
('3'/span>, numpy.str_)
或者,在你的情況下,f-string可以是你:
str = f "something here {df.index[i]} something here"
uj5u.com熱心網友回復:
當你把數字列的任何一個元素改為'string'時,整個列將變成'object'(在這里指'string'資料)。
你可以試試:
df.colA[0] = str(df.colA[0] )
# Then check the column 'colA' if its dtype is 'object'.
df.colA
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332401.html
標籤:
上一篇:提取URL查詢字串陣列regex
