在Python中设置浮点数可以通过以下几种方式:
直接赋值
my_float = 3.14
使用科学计数法
my_float = 6.022e23
强制类型转换
my_float = float(5) 将整数5转换为浮点数
使用`input()`函数
my_float = float(input("请输入一个浮点数:"))
使用`decimal`模块
from decimal import Decimal
getcontext().prec = 10 设置精度为10位小数
a = Decimal('0.1')
b = Decimal('0.2')
c = a + b 输出:0.3000,保留了4位小数的精度
处理数据框(DataFrame)中的浮点数
import pandas as pd
df = pd.DataFrame({'col1': ['3.14', '5.0', 'abc'], 'col2': [1, 2, 3]})
df['col1'] = df['col1'].astype(float) 将列 'col1' 转换为浮点数类型
以上方法可以帮助你在Python中设置浮点数。