building_block.style_number, _currency, _percent
数値のシーケンスを、可読性の高い文字列表現に整形します。
概要
style_number(): 固定小数点形式で数値を整形します。桁区切り記号を指定できます。style_currency(): 固定小数点形式で整形した数値の前に通貨記号を付与します(例: “$”)。style_percent(): 値を指定した倍率(既定値: 100)でスケーリングした後、パーセント記号などの単位記号を末尾に付与します。
style_number(
x: Sequence[int, float, np.number],
digits: int = 2,
big_mark: str = ","
) -> pd.Series
style_currency(
x: Sequence[int, float, np.number],
digits: int = 0,
symbol: str = "$",
big_mark: str = ","
) -> pd.Series
style_percent(
x: Sequence[int, float, np.number],
digits: int = 2,
unit: Union[int, float] = 100,
symbol: str = "%"
) -> pd.Series引数 Argument
x:scalar or array-like of int or float
整形対象となる数値の list もしくは pd.Seriesdigits:int
小数点以下の桁数。初期設定は以下の通りです。
style_number(): 2style_currency(): 0style_percent(): 2
big_mark:int
3桁毎の桁区切りに使用する記号。カンマ',', アンダーバー'_', もしくは 非表示''から選ぶことができます。symbol:str
貨幣記号を表す文字列unit:int or float
style_percent()において、整形前に値へ掛ける倍率。既定値は 100 です。
返り値 Value
以下の値をもつ pd.Series を返します。
building_block.style_number(): 任意の数値に対して、小数点以下を桁数digitsに丸め、3桁区切り記号を追加した値を文字列として返します。f-string によるフォーマットf'{x:{big_mark}.{digits}f}'を用いて実装されています。building_block.style_currency():build.style_number()と同じく任意の数値に対して、小数点以下を桁数digitsに丸め、3桁区切り記号と貨幣記号を追加した文字列を返します。f-string によるフォーマットf'{symbol}{x:{big_mark}.{digits}f}'を用いて実装されています。building_block.style_percent(): 任意の数値をパーセンテージ表示に変換した値を文字列として返します。f-string によるフォーマットf'{x*unit:.{digits}f}{symbol}'を用いて実装されています。
使用例 Examples
import numpy as np
from py4stats import building_block as build
x = [2000, 1000, 0.5, 0.11, 0.123]
print(build.style_number(x).to_list())
#> ['2,000.00', '1,000.00', '0.50', '0.11', '0.12']
print(build.style_number(x, big_mark = '').to_list())
#> ['2000.00', '1000.00', '0.50', '0.11', '0.12']
print(build.style_currency(x).to_list())
#> ['$2,000', '$1,000', '$0', '$0', '$0']pct = [0.11, 0.06, 0.05, 0.01, 0.00234]
print(build.style_percent(pct).to_list())
#> ['11.00%', '6.00%', '5.00%', '1.00%', '0.23%']
print(build.style_percent(pct, unit = 1).to_list())
#> ['0.11%', '0.06%', '0.05%', '0.01%', '0.00%']
print(build.style_percent(pct, unit = 1000, symbol = '‰').to_list())
#> ['110.00‰', '60.00‰', '50.00‰', '10.00‰', '2.34‰']