2 回答
TA贡献1830条经验 获得超9个赞
我对您的功能做了一些调整,现在它似乎可以工作了:)
def my_round(n, ndigits=1):
try:
part = n * 10 ** ndigits
delta = part - int(part)
# always round "away from 0"
if delta >= 0.5 or -0.5 < delta <= 0:
part = math.ceil(part)
else:
part = math.floor(part)
val = part/(10 ** ndigits)
except ValueError:
val = np.nan
return val
Data['Outcome'] = (Data['Numerator'].divide(Data['Denominator'])*100).apply(my_round)
print(Data)
输出:
AreaCode Numerator Denominator Outcome
0 101 300 1000 30.0
1 102 500 780 64.1
2 103 600 1100 54.5
3 104 667 1840 36.3
TA贡献1155条经验 获得超0个赞
将 pandas 导入笔记本后,您可以添加此行,以便您的浮点值不会超过小数点后一位:
import pandas as pd
pd.options.display.float_format = '{:.1f}'.format
添加回答
举报