Python针对float类型数值自定义精度(保留0)

import dask.dataframe as dd
import pandas as pd
from decimal import Decimal
# prepare data
data = {
    'name': ['Eason', 'Jay', 'JJ', 'Alice', 'yaya'],
    'id': [1000, 1001, 1002, 1003, 1004],
    'x': [1.9897712, -98.432490, -0.258545, 0.012345, 0.146248],
    'y': [1.9897347, -98.902596, -0.254255, 0.012345, 0.145124]
}
# create dask dataframe
df = dd.from_pandas(pd.DataFrame(data), npartitions=1)

decimal_dict = {'x': 0}
decimal_places = decimal_dict.get('x', 0)
decimal_places_len = "0." + "0" * decimal_places
def split_x(x, decimal_places_len):
    def format_decimal(x):
        return Decimal(x).quantize(Decimal(decimal_places_len))
    return x.map(format_decimal)

df['int_format_decimal'] = df['x'].map_partitions(split_x, decimal_places_len)
df = df.compute()
df

decimal_dict = {'x': 0}结果

decimal_dict = {'x': 2}结果

decimal_dict = {'x': 10}结果

import dask.dataframe as dd
import pandas as pd
from decimal import Decimal
# prepare data
data = {
    'name': ['Eason', 'Jay', 'JJ', 'Alice', 'yaya'],
    'id': [1000, 1001, 1002, 1003, 1004],
    'x': [1.9897712, -98.432490, -0.258545, 0.012345, 0.146248],
    'y': [1.9897347, -98.902596, -0.254255, 0.012345, 0.145124]
}
# create dask dataframe
df = dd.from_pandas(pd.DataFrame(data), npartitions=1)

decimal_dict = {'x': 10}
def format_decimal(x):
    decimal_places = decimal_dict.get('x', 0)
    decimal_places_len = "0." + "0" * decimal_places
    return Decimal(x).quantize(Decimal(decimal_places_len))

df['x_format_decimal'] = df['x'].map(format_decimal)
df = df.compute()
df

import dask.dataframe as dd
import pandas as pd
from decimal import Decimal
# prepare data
data = {
    'name': ['Eason', 'Jay', 'JJ', 'Alice', 'yaya'],
    'id': [1000, 1001, 1002, 1003, 1004],
    'x': [1.9897712, -98.432490, -0.258545, 0.012345, 0.146248],
    'y': [1.9897347, -98.902596, -0.254255, 0.012345, 0.145124]
}
# create dask dataframe
df = dd.from_pandas(pd.DataFrame(data), npartitions=1)

decimal_dict = {'x': 10, 'y': 0}
def format_decimal(x, decimal_places):
    format_string = '{:.' + str(decimal_places) + 'f}'
    return x.map(lambda val: format_string.format(val))

for column in ['x', 'y']:
    decimal_places = decimal_dict.get(column, 0)
    df[column] = df[column].map_partitions(format_decimal, decimal_places)
df = df.compute()
df

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值