我有以下数字:
import numpy as np
import pandas as pd
import datetime as dt
from urllib import urlretrieve
%matplotlib inline
url3='http://hopey.netfonds.no/tradedump.php?date= 20150523&paper=AAPL.O&csv_format=csv'
year = '2015'
month = '05'
days = ['20', '21', '22', '23', '24']
AAPL = pd.DataFrame()
for day in days:
AAPL = AAPL.append(pd.read_csv(url3 % (year, month, days), index_col=0, header=0, parse_dates=True))
AAPL.columns = ['bid', 'bdepth', 'bdeptht', 'offer', 'odepth', 'odeptht']
AAPL.info()
将这些数字中的任何一个转化为最佳功能是什么:
2333
380293
1230944
答案 0 :(得分:2)
si gem将为您处理此问题。首先安装它:
gem install si
然后只需拨打.si
号码:
2333.si #=> 2.33k
380293.si #=> 380k
1230944.si #=> 1.23M
答案 1 :(得分:-2)
[编辑:我最初给出binary表示而不是十进制表示(即除以1024而不是1000)。我已经得出结论,这可能是所需的数字解释,所以我修改了我的答案。另外,我也允许参数为负数,如@infused建议的那样,删除了"B"
中的"kB"
,"MB"
等,消除了舍入并添加了一个可选的精度参数]
你可以这样做:
NEG_ENDINGS = { "''"=>"''", 'k'=>'m', 'M'=>'μ', 'G'=>'n', 'T'=>'p',
'P'=>'f', 'E'=>'a', 'Z'=>'z', 'Y'=>'y' }
def convert(n, precision=0)
raise ArgumentError, "Absolute value too big!" if n.abs >= 10**27
raise ArgumentError, "Precision out-of-range" unless (0..3).cover?(precision)
if n.abs < 1000
"#{n}"
else
val = recurse(n.abs, precision)
(n >= 0) ? val : "-#{val[0..-2]}#{NEG_ENDINGS[val[-1]]}"
end
end
def recurse(n, precision, endings = %w|k M G T P E Z Y| )
suffix = endings.shift
x,n = n.divmod(1000)
if x < 1000
unless precision.zero?
x = "%.#{precision}f" % (x + n/1000.0)
end
return "#{ x }#{ suffix }"
end
recurse(x, precision, endings)
end
convert 233 #=> "233"
convert 768 #=> "768"
convert -841 #=> "-841"
convert 999 #=> "999"
convert 1_000 #=> "1k"
convert 2_833, 1 #=> "2.8k"
convert -2_833, 2 #=> "-2.83m"
convert 380_293 #=> "380k"
convert 1_230_944, 3 #=> "1.230M"
convert 12_309_446_125 #=> "12G"
convert -12_309_446_125 #=> "-12n"
convert 123_094_461_257_132_716 #=> "123P"
convert 123_094_461_257_132_716_324_625_917_831
#=> ArgumentError: Absolute value too big!