![](/img/trans.png)
[英]Python Pandas: Change type from float to float64 in a column which contains both numerical and string elements
[英]Change data type in text file from string to float Python
我正在嘗試讀取4列txt文件並創建第5列。 第1列是字符串,第2-4列是數字,但是它們被視為字符串。 我有兩個問題-我的python腳本當前無法在其中兩列上執行乘法運算,因為它正在讀取2-4列作為字符串。 我想知道如何將第2-4列(即數字)更改為浮點數,然后創建第5列,該列是前面兩列的乘積在一起。
您可以像這樣將字符串轉換為python中的float。
>>> float('1.25')
1.25
只需使用float(x)
方法將它們強制轉換為float即可,其中x是包含float的字符串
每當您遍歷行時,都可以通過try-except操作來運行字符串,例如:
try:
float(value)
except ValueError:
pass
#Or do some kind of error handling here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.