資料包含冒號(:)和空格,所以所有列的型別是物件。我怎樣才能將所有列的物件轉換為整數或浮點數。
我試著這樣做,但它沒有作業。
我試著這樣做。
df = pd.concat([df[col].str.split()
.str[0]
.str.replace(' :','')。 astype(float) for col in df] , axis=1)
df['col'] = df['col'].astype(int)
print(df)
AttributeError。只能使用.str accessor with string values!
uj5u.com熱心網友回復:
你可以使用regex來提取數字,然后在之后將其轉換為整數。請看下面的代碼:
span class="hljs-keyword">import re
def to_integer(x)。
result = re.findall('[0-9] ', str(x))
num = ''/span>.join(result)
return int(num)
df['col'] = df['col'].apply(to_integer)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/324474.html
標籤:
上一篇:用阿爾法影片繪制字串
